Java string processing, java string

Source: Internet
Author: User

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:


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.