Three methods of Java string interception (recommended)

As we all know, Java provides many ways to intercept strings. Let's take a look at some of them.

1. Split() + regular expression to intercept.

Pass the regular into split(). Returns a string array type. However, intercepting in this way will have a great performance loss, because analyzing regularization is very time-consuming.

Operation results:

abc 12 3yy98 0

2. Use substring() method to intercept the string.

Substring provides different interception methods through different parameters

2.1 only one parameter is passed

For example:

String sb = "bbbdsajjds"; sb. substring(2);

Truncate the string from the index number of 2 to the end of the string. (index value starts from 0);

2.2 pass in 2 index values

String sb = "bbbdsajjds"; sb. substring(2,4);

Start with index number 2 and end with index number 4 (excluding the interception of index number 4, that is, characters 2 and 3 are actually intercepted);

The operation results are as follows:

bdsajjds bd

3. Methods provided by stringutils

StringUtils. substringBefore(“dskeabcee”,“e”); / The result is: DSK / here is based on the first "e".

StringUtils. Substringbeforelast ("dskeabcee", "e") the result is: dskeabcee

The last "e" shall prevail here.

The above three methods of Java string interception (recommended) are all the contents shared by Xiaobian. I hope to give you a reference and support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>