3

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?

Surender Thakran
4,05612 gold badges52 silver badges81 bronze badges
asked Mar 22, 2010 at 11:54

5 Answers 5

8
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

answered Mar 22, 2010 at 11:56
Sign up to request clarification or add additional context in comments.

Comments

2

you should identify the particular "a" to remove try thisa.replace(" a ", " ");

answered Mar 22, 2010 at 11:58

Comments

1

You were asking to replace every 'a', and that is what you got.

Some solutions:

  • replaceAll(" a ", " ") (note the spaces around the 'a')
  • use regex to chek for 'a' surrounded by word barriers (\b if I'm not mistaken)
answered Mar 22, 2010 at 11:58

Comments

0

Try using this:

String str = "Yeahh, I have no a idea what's happening now!";

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

answered May 3, 2016 at 11:36

Comments

-2
a.replace("Yeahh", "Yehh");

Is it just replacing the a in Yeah? or the first a in the string?

answered Mar 22, 2010 at 12:03

1 Comment

he wants to replace the "a" in "no a idea"

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.