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"