简体   繁体   中英

Unwanted elements appearing when splitting a string with multiple separators in Java

I have a string from which I need to remove all mentioned punctuations and spaces. My code looks as follows:

    String s = "s[film] fever(normal) curse;";
    String[] spart = s.split("[,/?:;\\[\\]\"{}()\\-_+*=|<>!`~@#$%^&\\s+]");
    System.out.println("spart[0]: " + spart[0]);
    System.out.println("spart[1]: " + spart[1]);
    System.out.println("spart[2]: " + spart[2]);
    System.out.println("spart[3]: " + spart[3]);
    System.out.println("spart[4]: " + spart[4]);

But, I am getting some elements which are blank. The output is:

spart[0]: s
spart[1]: film
spart[2]:
spart[3]: fever
spart[4]: normal

My desired output is:

spart[0]: s
spart[1]: film
spart[2]: fever
spart[3]: normal
spart[4]: curse

Try with this:

public static void main(String[] args) {
    String s = "s[film] fever(normal) curse;";
    String[] spart = s.split("[,/?:;\\[\\]\"{}()\\-_+*=|<>!`~@#$%^&\\s]+");
    for (String string : spart) {
        System.out.println("'"+string+"'");
    }
}

output:

's'
'film'
'fever'
'normal'
'curse'

I believe it is because you have a Greedy quantifier for space at the end there. I think you would have to use an escape sequence for the plus sign too.

String spart = s.replaceAll( "\\W", " " ).split(" +");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM