Java indexof () simple character search instance
Int indexof (string ch );
It is to find the position of the character/string ch after index. If it is not found, the return value is-1. The index can be absent. If it is not found, the default value is 0.
Eg: string str = "liuzheliuxing ";
System. out. println (int) 'I') // ascii of I
System. out. println (str. indexof (105); // The ascii value of I is.
Result: 1
Eg: string str = "liuzheliuxing ";
System. out. println (str. indexof ('I', 2 ));
Result: 7
Application: determines whether a parameter is of the numeric type.
Public boolean isnumber (string str ){
String number = "103194825611 ";
For (int I = 0; I <str. length (); I ++ ){
If (number. indexof (str. charat (I) =-1 ){
Return false;
}
}
Return true;
}
View instances
Public class mainclass
{
Public static void main (string args [])
{
String letters = "abcdefghijklmabcdefghijklm ";
System. out. printf (
"'C' is located at index % dn", letters. indexof ('C '));
System. out. printf (
"'A' is located at index % dn", letters. indexof ('A', 1 ));
System. out. printf (
"'$' Is located at index % dnn", letters. indexof ('$ '));
}
}