Java string processing

Source: Internet
Author: User

http://blog.csdn.net/pipisorry/article/details/43303145

Java judgment string contains a character segment

1. Using the IndexOf () method

String str = "1234567";

if (Str.indexof ("23")! =-1) {
System.out.println ("contains this string");

}


public void Test1 () {

String string = "Good morning Everyone";
String sub = "Morning";
int a = String.IndexOf (sub);

if (a >= 0) {
System.out.println ("Morning position in the string:" + a);
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 ("Deleted characters are:" + SS2);
} else {
System.out.println ("does not exist");
}
}


2. Using the Repalce () method

public class Test {public  static void Main (string[] args) {   String S1 = "abc";   String s2 = "1111abcdefe";    if (S2.contains (S1)) {    System.out.println ("S2 contains S1");    Remove 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


Java string processing

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.