Krishnendu Ghosh Krishnendu Ghosh - 3 months ago 8
Java Question

Digits are getting deleted when splitting a string

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]);


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]);


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

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

Answer
String[] spart = s.split("[,/?:;\\[\\]\"{}()\\-_+*=|<>!`~@#$%^&\\s]+");