-2

I want to replace multiple character in given string with a single space.

eg: He is a very very good boy, isn't he? Should be replaced to

He
is
a
very
very
good
boy
isn
t
he 

My code is

String str = "He is a very very good boy, isn't he?"
String str2 = str.replaceAll("![,?.\_'@+] +"," ");
 String []tokens = str2.split(" +");

for(int i = 0; i< tokens.length; i++)
       System.out.println(tokens[i]);

But the output is

He
is
a
very
very
good
boy,
isn't
he?

Please correct my code if possible or suggest a new one.

Cristik
  • 30,989
  • 25
  • 91
  • 127
ra1vi2
  • 17
  • 6
  • If you are trying to splt the text into words, then you should NOT split `"isn't"` into `"isn" "t"`. The apostrophe is part of the word. – Stephen C Jan 23 '16 at 23:44

4 Answers4

2

Here is a great article: Replace All Method

If you want to only have letters in your string, and replace everything else with a space, here is the statement:

str2 = str.replaceAll("[^a-zA-z]", " ");

If you want to replace the characters you mentioned (excluding brackets), use this:

s1 = s1.replaceAll("[^[!,?._'@+]]", " ");

If you want to include the brackets, use this:

s1 = s1.replaceAll("[^[!,?._'@+\\[\\]]]", " ");

Hope this helps!

z7r1k3
  • 718
  • 1
  • 5
  • 20
  • Space or any of the special characters like ![,?.\_'@+] will act as a delimiter. Rest all characters are allowed. – ra1vi2 Jan 24 '16 at 02:07
  • Do you want to exclude the [] brackets too? – z7r1k3 Jan 24 '16 at 02:25
  • Using s1 = s1.replaceAll("[^[!,?._'@+]]", " "); will remove the characters that are inside the brackets, and s1 = s1.replaceAll("[^[!,?._'@+\\\[\\\]]]", " "); will remove square brackets as well. – z7r1k3 Jan 24 '16 at 02:35
1

First of all, I think your regular expression is missing an escape. I think you wanted to include the back slash \ in your expression, but you have to escape it like this: \\.

Moreover, your regular expression "![,?.\_'@+] +" indicates that you want to replace a combination of an exclamation mark !, any character that is part of the set containing ,, ?, ., \, _, ', @, and +, and one or more spaces (+) by a single space.

The regular expression [,'?]+ should do the job. You can read more about how to form the correct expression at Regular expressions.

Gyzuh
  • 171
  • 1
  • 1
  • 5
0

If you want to replace only with alphabets then

String x = sequence.replaceAll("[^a-zA-Z] +"," ");

and with alphabet and digits

String y = sequence.replaceAll("[^a-zA-Z0-9] +"," ");

Have a look at Replace() and ReplaceAll() methods

Farrukh Faizy
  • 1,203
  • 2
  • 18
  • 30
  • Space or any of the special characters like ![,?.\_'@+] will act as a delimiter. Rest all characters are allowed. – ra1vi2 Jan 24 '16 at 02:08
0

I tried all the above but this one worked.

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String S = br.readLine();
List<String> splitted = new ArrayList<>(Arrays.asList(S.split("[ :#$%^&/<>{}*|;_()!,?.'@+\\\\\\[\\]\\\"]+")));
splitted.removeAll(Arrays.asList(null,""));
System.out.println(splitted.size());
for (String word: splitted) System.out.println(word);

this is solution for the whole problem which I have posted.

ra1vi2
  • 17
  • 6