JAVA three methods of intercepting strings subString, StringUtils, split
There are mainly the following methods:
1. Use the subString() method to perform string interception (most commonly used)
2. Methods provided by StringUtils
3. Split() + regular expression to intercept
1. Use the subString() method to intercept strings and return substrings in strings. There are two uses in Java
The first type is to pass a parameter:
public String substring(int beginIndex) //This substring starts with the character at the specified index until the end of the string. public String substring(int beginIndex) //This substring starts with the character at the specified index,Until the end of this string。
The second type is to pass two parameters:
public String substring(int beginIndex, int endIndex) //From the specified beginIndex Beginning,Until the index endIndex - 1 Characters at。therefore,The length of the substring is endIndex-beginIndex。
//Parameter description: beginIndex -- Start index (including), endIndex -- End index (excluding).
Sample code:
public static void main(String[] args) { String Str = new String("hello wrold"); ("Return value, intercepted from bit 4 to the end of the string:" ); ((4) ); ("Return value, intercepting from bit 4 to bit 10:" ); ((4, 10) ); }
Running results
Return value, intercepted from bit 4 to the end of the string: o whole
Return value, intercepting from bit 4 to bit 10 : o wrol
2. Methods provided by StringUtils
//The effect is the same as the first method("hello world", 4); // Return value, intercepted from bit 4 to the end of the string: o whole("hello world", 4, 10); // Return value, intercept from bit 4 to bit 10: o wrol//Seave the characters before a string("hello world", "l"); //The result is: he here is the first "l", as the standard.("hello world", "l"); //The result is: hello wor Here the last "l" is the basis.//Seave the characters after a string("hello world", "l"); //The result is: lo world Here is the first "l", which is the standard.("hello world", "l"); //The result is: d Here the last "l" is the basis.//Seave characters separated between two strings("hello world", "o"); //The result is: w The string between two os.("hello world", "l", "r"); //The result is: lo wo string between the first character "l" and the first character "r"("hello world", "l", "r"); //turn out:Array [lo wo] The first character“l”与The first character“r”String between,以Array形式返回。
Use StringUtils to import common-lang3 package
<dependency> <groupId></groupId> <artifactId>commons-lang3</artifactId> <version>3.12.0</version> </dependency>
3. Split() + regular expression to intercept
Snipped strings as splitters and returned as arrays
String str = "hello, name, 12345, 6789"; String[] strs=(","); for(int i=0,len=;i<len;i++){ (strs[i].toString()); }
Running results
hello
name
12345
6789
This is the article about JAVA intercepting strings. This is all about subString, StringUtils, and split. For more related Java intercepting string content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!