Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String replaceAll method (Java)

Tags:

java

string

I have following problem,

Code:

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

Before removing 'a', result:

Yeahh, I have no a idea what's happening now!

Actual Result: After removing 'a', result:

Yehh, I hve no ide wht's hppening now!

Desired Result:

Yeahh, I have no idea what's happening now!

Anyone can give me some advices to achieve my desired result?

like image 307
Mr CooL Avatar asked Apr 27 '26 09:04

Mr CooL


1 Answers

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

You don't need the replaceAll(..) method - it is if you want to use regex, and you don't need it, at least for this example.

If you need this for more complex examples than shown, then use replaceAll(..) and take a look at java.util.regex.Pattern. For example, matching all whitespace characters is done using \s

like image 81
Bozho Avatar answered Apr 29 '26 23:04

Bozho



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!