How to split String into some separator but not removing that separator in Java?

I ran into String splitting problem.

I want to split String into some separator, but without losing this separator.

When we use the somestring.split(String separator) method in Java, it breaks the String , but removes the separation from the String . I do not want this to happen.

I want to get the result as shown below:

 String string1="Ram-sita-laxman"; String seperator="-"; string1.split(seperator); 

Output:

 [Ram, sita, laxman] 

but I want to get a result similar to the one below:

 [Ram, -sita, -laxman] 

Is there any way to get this conclusion?

+53
java string split regex
Dec 11 '10 at 11:12
source share
5 answers
 string1.split("(?=-)"); 

This works because split actually accepts a regular expression . What you actually see is a “positive result of a zero-width search”.

I would like to explain more, but my daughter wants to play tea. :)

Edit: Back!

To explain this, I will first show you another split operation:

 "Ram-sita-laxman".split(""); 

This splits your line on each line of zero length. There is a zero-length string between each character. Therefore, the result:

 ["", "R", "a", "m", "-", "s", "i", "t", "a", "-", "l", "a", "x", "m", "a", "n"] 

Now I change my regular expression ( "" ) only to match strings of zero length if followed by a dash.

 "Ram-sita-laxman".split("(?=-)"); ["Ram", "-sita", "-laxman"] 

In this example ?= Means "lookahead". In particular, this means a “positive outlook”. Why is it positive? Because you can also have a negative lookahead ( ?! ) That will break into every line of zero length that is not , and then a dash:

 "Ram-sita-laxman".split("(?!-)"); ["", "R", "a", "m-", "s", "i", "t", "a-", "l", "a", "x", "m", "a", "n"] 

You can also have a positive lookbehind ( ?<= ) That will be split into every line of zero length preceded by a dash:

 "Ram-sita-laxman".split("(?<=-)"); ["Ram-", "sita-", "laxman"] 

Finally, you can also have a negative lookbehind ( ?<! ) That will break into every line of zero length that is not before which a dash indicates:

 "Ram-sita-laxman".split("(?<!-)"); ["", "R", "a", "m", "-s", "i", "t", "a", "-l", "a", "x", "m", "a", "n"] 

These four expressions are known collectively as "inverse expressions."

Bonus: combining them

I just wanted to show an example that I came across recently that combines two kinds of searches. Suppose you want to split the CapitalCase identifier into your tokens:

 "MyAwesomeClass" => ["My", "Awesome", "Class"] 

You can accomplish this using this regex:

 "MyAwesomeClass".split("(?<=[az])(?=[AZ])"); 

This is split into every zero-length string, preceded by a lowercase letter ( (?<=[az]) ), followed by an uppercase letter ( (?=[AZ]) ).

This method also works with camelCase identifiers.

+153
Dec 11 '10 at
source share

This is a bit dodgy, but you can introduce a dummy delimiter using the replace function. I don't know Java methods, but in C # it could be something like:

 string1.Replace("-", "#-").Split("#"); 

Of course, you will need to select a dummy delimiter that will not be anywhere else in the line.

+3
Dec 11 '10 at 11:18
source share

To do this, split the line, and then add a separator at the beginning of each extracted line, except the first.

+2
Dec 11 '10 at 11:16
source share
 seperator="-"; String[] splitstrings = string1.split(seperator); for(int i=1; i<splitstring.length;i++) { splitstring[i] = seperator + splitstring[i]; } 

that is, code suitable for a LadaRaider response.

+1
Dec 11 '10 at
source share

Adam hit a nail on the head! I used his answer to figure out how to insert the text of the file name from the browser of the file dialog into the rich text box. The problem I ran into was adding a new line to the "\" line in the file line. The string.split command split into \ and deleted it. After using a combination of Ada code, I was able to create a new line after each \ in the file name.

Here is the code I used:

 OpenFileDialog fd = new OpenFileDialog(); fd.Multiselect = true; fd.ShowDialog(); foreach (string filename in fd.FileNames) { string currentfiles = uxFiles.Text; string value = "\r\n" + filename; //This line allows the Regex command to split after each \ in the filename. string[] lines = Regex.Split(value, @"(?<=\\)"); foreach (string line in lines) { uxFiles.Text = uxFiles.Text + line + "\r\n"; } } 

Enjoy it!

Walrusking

0
Feb 20 '13 at 18:03
source share



All Articles