String replaceAll Method (Java)

I have the following problem:

the code:

String a="Yeahh, I have no a idea what happening now!";
System.out.println(a);
a=a.replaceAll("a", "");
System.out.println(a);

Before deleting 'a', the result is:

Yes, I have no idea what is happening now!

Actual result: After removing "a", the result:

Yehh, I don’t see what is happening now!

Desired Result:

Yes, I have no idea what is happening now!

Can anyone give me some tips to achieve the desired result?

+3
source share
5 answers
a = a.replace(" a ", " ");

You do not need a method replaceAll(..)- this is if you want to use a regular expression, and you do not need it, at least for this example.

, , replaceAll(..) java.util.regex.Pattern. , \s

+8

"a" a.replace(" a ", " ");

+2

"a", , .

:

  • replaceAll ( "a", "") ( 'a')
  • regex chek 'a', (\ b, )
+1

:

String str = ", , !";

String newStr = str.replaceAll( "a", "");

0
a.replace("Yeahh", "Yehh");

? a ?

-2

Source: https://habr.com/ru/post/1738020/


All Articles