Java string processing, java string
Http://blog.csdn.net/pipisorry/article/details/43303145
Java judges that a string contains a certain character segment
1. Use the indexOf () method
String str = "1234567 ";
If (str. indexOf ("23 ")! =-1 ){
System. out. println ("containing this string ");
}
Public void test1 (){
String string = "good morning everyone ";
String sub = "morning ";
Int a = string. indexOf (sub );
If (a> = 0 ){
System. out. println ("Location of morning in the string:" + );
String ss1 = string. substring (0, a + sub. length ());
String ss2 = string. substring (a + sub. length (), string. length ());
System. out. println ("the result you need is:" + ss1 );
System. out. println ("the characters deleted are:" + ss2 );
} Else {
System. out. println ("nonexistent ");
}
}
2. Use the repalce () method
Public class Test {public static void main (String [] args) {String s1 = "abc"; String s2 = "1111 abcdefe"; if (s2.contains (s1) {System. out. println ("s2 contains s1"); // Delete s1 s2 = s2.replace (s2.substring (s2.indexOf (s1), s2.length (), ""); System. out. println (s2);} else {System. out. println ("s2 does not contain s1 ");}}}
From: http://blog.csdn.net/pipisorry/article/details/43303145
Ref: