1

What is the best way to compare that given String start With another String

Example :

  if("ABCD1234".startsWith("ABCD")){
        System.out.println("This is true ");
    }

    if("ABCD1234".substring(0,4).equalsIgnoreCase("ABCD")){
        System.out.println("This also true ");
    }

Or are there any other solutions ?

1
  • 2
    What is the best way... Define "good". Commented Nov 7, 2012 at 10:53

8 Answers 8

5

I think startsWith() is the best option since it makes your intention clear to anyone reading your code. If you want it to be case-insensitive convert both values to the same case before calling the method.

String prefix = "ABCD";
prefix = prefix.toLowerCase();

if("ABCD1234".toLowerCase().startsWith(prefix)) {
    System.out.println("This is true ");
}

If you want to be extra careful you may want to pass a Locale to toLowerCase() or at least be sure to call it on both Strings to ensure your code passes the Turkey Test.

Sign up to request clarification or add additional context in comments.

Comments

4

Another solution would be:

if (s.indexOf("ABCD") == 0) {
  ...
}

However, I would prefer your first choice.

Comments

1

Use of equalsIgnoreCase could be better in case of case insensitive equals but before substring you should check length and it will also create a new String in memory String pool.

Another way like it

    if("ABCD1234".substring(0,4).compareTo("ABCD")==0){
        System.out.println("This also true ");
    }
    if("ABCD1234".substring(0,4).compareToIgnoreCase("ABCD")==0){
        System.out.println("This also true ");
    }

But I think startsWith is better option here but make sure case sensitivity.

Comments

1

also possible:

"ABCD1234".matches("ABCD.*");

Many other crazy solutions in my head :) IMHO the "best" way is to use your first suggestions.

Comments

1

You could use the StringUtils from Apache Commons:

StringUtils.startsWith("abcdef", "abc") = true
StringUtils.startsWith("ABCDEF", "abc") = false
StringUtils.startsWithIgnoreCase("ABCDEF", "abc") = true

http://commons.apache.org/lang/api-2.5/org/apache/commons/lang/StringUtils.html

3 Comments

Why would you use a 3rd party library for the functionality that is already available in pre-defined one?
You are right with respect to the first method because "ABCD1234".startsWith("ABCD") is much clearer. But if you have to ignore the case I would prefer using StringUtils.startsWithIgnoreCase("ABCDEF", "abc") beacuse the intention is much clearer and it reads much better than for example "ABCD1234".substring(0,4).equalsIgnoreCase("ABCD"). You are right, adding the dependency just for one line of code might be exaggerated. But my experience is that when you have to deal with strings in one place of your system you probably will deal with it in other places as well.
@Felix.. Agreed with the last line, and hence with the 2nd last line too. :)
1

to be honest, first you should ask yourself is "abcd" equals "ABCD"?

if not, then i think first solution is much better, it is more cleaner and readable

if your answear is yes, then obviously, you cant use first solution

Comments

1

I will prefer 2nd choice:

if("ABCD1234".substring(0,4).equalsIgnoreCase("ABCD")){
        System.out.println("This also true ");

In this, u can additionally check by conbverting your string to LowerCase.

if(string.toLowerCase().startswith(string2))

Comments

0

When I have to write a Java app for work I use the startsWith approach.

3 Comments

The language is stated: java.
The question is tagged Java.
Sorry didn't have my coffee. But yes I would still use startsWith!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.