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 ?
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.If you want to be extra careful you may want to pass a
LocaletotoLowerCase()or at least be sure to call it on both Strings to ensure your code passes the Turkey Test.