0
public class Bla { public static void main(String[] args) { String s = "/sdsaads/gtagrf/eredsa"; System.out.println(s.replace("a$", "")); } } 

I want to erase the "a" in the end of the string. When I delete the $, all the "a" disappears, but when I put it, nothing occurs. Explanation ?

2 Answers 2

1

Its replaceAll() that takes regular expression as first parameter.

System.out.println(s.replaceAll("a$", "")); 
Sign up to request clarification or add additional context in comments.

Comments

1

The replace() method doesn't expect a regex expression as parameter:

String java.lang.String.replace(CharSequence target, CharSequence replacement) 

You should use replaceAll() which expects a regex as parameter:

String java.lang.String.replaceAll(String regex, String replacement) 

So your code would be:

System.out.println(s.replaceAll("a$", "")); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.