Contents

Methods For Split String

Using split()

Pass the regular into split(). The return is a String[] type. However, intercepting in this way will have a large performance penalty, because analyzing the regularity is very time-consuming.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
String str = "123123123@gmail.com";
String[] strs = str.split("@");
for (int i = 0; i < strs.length; i++) {
	System.out.println(strs[i]);
}

/**
 * result:
 * 123123123
 * gmail.com
 * /

Using subString() to intercept string

  • Only use beginIndex
1
2
3
4
5
6
7
8
String str = "123123123@gmail.com";
String str_ = str.substring(2);
System.out.println(str_);

/**
 * result:
 * 3123123@gmail.com
 * /
  • Using beginIndex and endIndex
1
2
3
4
5
6
7
8
String str = "123123123@gmail.com";
String str_ = str.substring(2, 9);
System.out.println(str_);

/**
 * result:
 * 3123123
 * /
  • Based on a character
1
2
3
4
5
6
7
8
String str = "123123123@gmail.com";
String str_ = str.substring(2, str.indexOf("@"));
System.out.println(str_);

/**
 * result:
 * 3123123
 * /