Use String.split() with multiple delimiters

JavaRegex

Java Problem Overview


I need to split a string base on delimiter - and .. Below are my desired output.

AA.BB-CC-DD.zip ->

AA
BB
CC
DD
zip 

but my following code does not work.

private void getId(String pdfName){
    String[]tokens = pdfName.split("-\\.");
}

Java Solutions


Solution 1 - Java

I think you need to include the regex OR operator:

String[]tokens = pdfName.split("-|\\.");

What you have will match:
[DASH followed by DOT together] -.
not
[DASH or DOT any of them] - or .

Solution 2 - Java

Try this regex "[-.]+". The + after treats consecutive delimiter chars as one. Remove plus if you do not want this.

Solution 3 - Java

You can use the regex "\W".This matches any non-word character.The required line would be:

String[] tokens=pdfName.split("\\W");

Solution 4 - Java

Using Guava you could do this:

Iterable<String> tokens = Splitter.on(CharMatcher.anyOf("-.")).split(pdfName);

Solution 5 - Java

The string you give split is the string form of a regular expression, so:

private void getId(String pdfName){
    String[]tokens = pdfName.split("[\\-.]");
}

That means to split on any character in the [] (we have to escape - with a backslash because it's special inside []; and of course we have to escape the backslash because this is a string). (Conversely, . is normally special but isn't special inside [].)

Solution 6 - Java

For two char sequence as delimeters "AND" and "OR" this should be worked. Don't forget to trim while using.

 String text ="ISTANBUL AND NEW YORK AND PARIS OR TOKYO AND MOSCOW";
 String[] cities = text.split("AND|OR"); 

Result : cities = {"ISTANBUL ", " NEW YORK ", " PARIS ", " TOKYO ", " MOSCOW"}

Solution 7 - Java

I'd use Apache Commons:

import org.apache.commons.lang3.StringUtils;

private void getId(String pdfName){
    String[] tokens = StringUtils.split(pdfName, "-.");
}

It'll split on any of the specified separators, as opposed to StringUtils.splitByWholeSeparator(str, separator) which uses the complete string as a separator

Solution 8 - Java

String[] token=s.split("[.-]");

Solution 9 - Java

pdfName.split("[.-]+");

  • [.-] -> any one of the . or - can be used as delimiter

  • + sign signifies that if the aforementioned delimiters occur consecutively we should treat it as one.

Solution 10 - Java

It's better to use something like this:

s.split("[\\s\\-\\.\\'\\?\\,\\_\\@]+");

Have added a few other characters as sample. This is the safest way to use, because the way . and ' is treated.

Solution 11 - Java

You may also specified regular expression as argument in split() method ..see below example....

private void getId(String pdfName){
String[]tokens = pdfName.split("-|\\.");
}

Solution 12 - Java

Try this code:

var string = 'AA.BB-CC-DD.zip';
array = string.split(/[,.]/);

Solution 13 - Java

s.trim().split("[\\W]+") 

should work.

Solution 14 - Java

If you know the sting will always be in the same format, first split the string based on . and store the string at the first index in a variable. Then split the string in the second index based on - and store indexes 0, 1 and 2. Finally, split index 2 of the previous array based on . and you should have obtained all of the relevant fields.

Refer to the following snippet:

String[] tmp = pdfName.split(".");
String val1 = tmp[0];
tmp = tmp[1].split("-");
String val2 = tmp[0];
...

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionThang PhamView Question on Stackoverflow
Solution 1 - JavaRichard HView Answer on Stackoverflow
Solution 2 - JavaPeter KnegoView Answer on Stackoverflow
Solution 3 - JavaVarun GangalView Answer on Stackoverflow
Solution 4 - JavaColinDView Answer on Stackoverflow
Solution 5 - JavaT.J. CrowderView Answer on Stackoverflow
Solution 6 - JavaÖMER TAŞCIView Answer on Stackoverflow
Solution 7 - JavaEddView Answer on Stackoverflow
Solution 8 - JavaNitishView Answer on Stackoverflow
Solution 9 - JavaTryingView Answer on Stackoverflow
Solution 10 - JavaPritam BanerjeeView Answer on Stackoverflow
Solution 11 - JavaAvdhesh YadavView Answer on Stackoverflow
Solution 12 - JavaReaperView Answer on Stackoverflow
Solution 13 - JavasssView Answer on Stackoverflow
Solution 14 - JavaisometrikView Answer on Stackoverflow