Reputation: 112
I know this question has been asked to death but I have tried all solutions that were given to 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.
Upvotes: 0
Views: 129
Reputation: 20875
This works as expected indeed
for (String token : "Something*like*this*".split("\\*")) {
if (token.equals("this")) {
System.out.println("Found this");
}
}
Upvotes: 2
Reputation: 12484
The if
will execute here.
Test this in drJava interactions pane(or your fav IDE pane), the individual pieces(to check they each work).
Welcome to DrJava. Working directory is C:\JavaProjects
> String s = "Something*like*this";
> String[] sarray = s.split("\\*");
> sarray[0]
"Something"
> sarray[1]
"like"
> sarray[2]
"this"
> sarray[3]
java.lang.ArrayIndexOutOfBoundsException
> sarray.length
> 3
> sarray[1].equals("this")
false
>
Upvotes: 0