Brief Introduction to Java String Split "simple introduction to Java string Split"

Source: Internet
Author: User

Split is a common function in Java. It split a full string to an array based on Delimeter.

For example, Split ' A:b:c ' with ': ' results in [A, B, c]

In some scenario, it's better to keep the delimeter instead for discard it while splitting.

Here is some strategies.

Split is a common function in Java that splits the entire string into arrays based on delimiters.

For example "A:b:c" through the ":" section to get the array [A, B, c]

However, under certain scenarios, we might want to keep the delimiter

Here are some ways to preserve separators

System.out.println (arrays.tostring ("A:b:c". Split (":"));  Normal split
[A, B, c]
System.out.println (arrays.tostring ("A:b:c". Split ("(=:)")); Look behind
[A,: B,: c]
System.out.println (arrays.tostring ("A::b:c". Split ("(=:)")); Look behind
[A,:,: B,: c]
System.out.println (arrays.tostring ("A:b:c". Split ("(? <=:)")); Look ahead
[A:, B:, C]
System.out.println ("A:b:c". Split ("(?!:)"))); Look ahead
[A:, B:, C]
System.out.println (arrays.tostring ("A:b:::c". Split ("?! =:)"))); Look Bothway
[A,:, B,:,:,:, C]
System.out.println (arrays.tostring ("A:b:::c". Split ("(? <=:) | (? =:)"))); Look Bothway
[A,:, B,:,:,:, C]

Look ahead forward binding

Delimeter'll be attached to the previous string

The delimiter is appended to the forward string

Look behind back to union

Delimeter'll be attached to the subsequent string

The delimiter is appended to the forward string

Look Bothway completely detached

Similar to normal split, but every delimeter'll be is included in the array

Similar to normal segmentation, but each delimiter also appears in the array

Some Interesting usage

Some interesting usage.

System.out.println (arrays.tostring ("1A2BB3CCC". Split ("(? <=[a-z])" (? =[0-9])));  Digit + [a-z]characters
[1a, 2BB, 3CCC]
System.out.println (arrays.tostring ("1_1112_222aditional3_333". Split ("(? <=_.)")); "_" with 2 more chars
[1_11, 12_22, 2aditional3_33, 3]
System.out.println (arrays.tostring ("1_1112_222aditional3_3333". Split ("(? <=_.{ 3})); "_" with 3 more chars
[1_111, 2_222, aditional3_333, 3]
System.out.println (arrays.tostring ("1_1112_222aditional3_33". Split ("(? <=_.{ 3})); "_" with 3 more chars
[1_111, 2_222, aditional3_33]

Brief Introduction to Java String Split "simple introduction to Java string Split"

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.