Tuesday 2 January 2018

String comparison solutions in java not working

itemprop="text">

I know this question has been asked to
death but I have tried all solutions that were given to href="https://stackoverflow.com/questions/658953/if-statement-with-string-comparison-fails">this
question and my if statement still doesn't execute..
My code goes
like this:



String s =
"Something*like*this*";

String[] sarray =
s.split("\\*");

for(int i = 0; i < sarray.length; i++)
{
if(sarray[i].equals("this")) {
//do something

}
}


Any
suggestions would be greatly appreciated.




Answer




This works as expected
indeed



for (String token :
"Something*like*this*".split("\\*")) {
if (token.equals("this")) {

System.out.println("Found this");

}
}

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...