6

I tried to replace "-" character in a Java String but is doesn't work :

str.replace("\u2014", "");

Could you help me ?

1
  • 3
    str = str.replace("\u2014", ""); Commented Jul 20, 2013 at 11:24

3 Answers 3

13

String is Immutable in Java. You have to reassign it to get the result back:

String str ="your string with dashesh";
str= str.replace("\u2014", "");

See the API for details.

Sign up to request clarification or add additional context in comments.

Comments

5

this simply works..

    String str = "String-with-dash-";
    str=str.replace("-", "");
    System.out.println(str);

output - Stringwithdash

Comments

0

It's quite easy. You can use an Apache library, that will be useful while you develop an application. This is apache-commons-lang. You can do the following:

public class Main {

    public static void main(String[] args) {

        String test = "Dash - string";
        String withoutDash = StringUtils.replace(test, "-", "");
        System.out.println(withoutDash);
    }

}

2 Comments

the apache commons lang library is a bit overkill to replace a dash in a string, java manages fine on its own
Yes, but did you see my comment ? "that will be useful while you develop an application" That's why I proposed this solution

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.