String str = "Hello World";
if(str.contains("World")){
int index = str.indexOf("World");
System.out.println("Index of 'World': " + index);
} else {
System.out.println("'World' not found");
}
String str = "Hello World";
char target = 'W';
int index = -1;
for(int i=0; i<str.length(); i++){
if(str.charAt(i) == target){
index = i;
break;
}
}
System.out.println("Index of 'W': " + index);
String str = "Hello World";
char[] charArray = str.toCharArray();
char target = 'W';
int index = -1;
for(int i=0; i<charArray.length; i++){
if(charArray[i] == target){
index = i;
break;
}
}
System.out.println("Index of 'W': " + index);