Several ways in which Java intercepts strings

Source: Internet
Author: User

A lot of string interception is provided in Java. Here's a look at a few of them.
1.split () + regular expression to intercept.
The regex is passed into split (). Returns a string array type. However, interception in this way can have a significant performance loss because the analysis is time-consuming.

String str = "abc,11,abc123,1"; String[]  strs=str.split (",");  for (int i=0,len=strs.length;i<len;i++) {    System.out.println (strs[i].tostring ());}

Operation Result:

abc11abc1231

  

2. Use the substring () method for string interception.
SUBSTRING provides different interception methods through different parameters.
2.1 Pass only one parameter
For example:

String sb = "abcdef122d";    Sb.substring (2);

 

Starts the string from the index number 2 and continues to the end of the string. (index value starts from 0);
2.2 Incoming 2 index values

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

  

Starting from index number 2 to index good 4 end (and do not include index 4 interception, that is, the actual interception of 2 and 3rd characters);
The results of the operation are as follows:

Cdef122dcd

  

3. Methods provided through the StringUtils
Stringutils.substringbefore ("Dskeabcee", "E");
/ Result: DSK/
Here is the first "E", as the standard.

Stringutils.substringbeforelast ("Dskeabcee", "E")
The result is: DSKEABCE
This is the last "E" to prevail.

Several ways in which Java intercepts strings

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.