1

I wonder if there is a way to use replace() with multiple replacement in one line in java. for example:

String precio_gold = "59,99$";
precio_gold = precio_gold.replace(",", ".");
precio_gold = precio_gold.replace("$", "");

And also if I there is a way to introduce more posibilites in the first "argument" of replace method, for example receive a price and depending if it has $ or € replace with "" (nothing):

precio_gold = precio_gold.replace("$" AND "€", ""); //This is wrong but I put just to see what I want to say.
5
  • 3
    Use replaceAll instead of replace, which takes a regular expression as its first argument (instead of a literal string to replace): precio_gold.replaceAll("\\$|€", ""); Commented Jan 12, 2017 at 10:41
  • Better regex : [$€] ($ has no special meaning in a character class so it does not need to be escaped) Commented Jan 12, 2017 at 10:44
  • @Jesper : added an answer quite similar to your comment... Commented Jan 12, 2017 at 10:45
  • 1
    We're talking regex, but you should instead be using a localized NumberFormat. See this SO question Commented Jan 12, 2017 at 10:52
  • Rather than always replacing "," with ".", I suggest you output the decimal point using a Locale-dependent formatter instead. If you hard-code it to ".", sooner or later some European will complain that your output is wrong. Commented Jan 12, 2017 at 10:53

5 Answers 5

4

For your first problem, doing multiple replaces on one line, you could just chain the method.

System.out.println(precio_gold.replace(",", ".").replace("$", ""));

For your second problem, you could use replaceAll with a regex.

System.out.println(precio_gold.replaceAll("\\$|€", ""));

Note that $ is escaped with \\ since it's a special character in a regular expression.

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

1 Comment

Worked for both cases! Thanks to all the replies!
2

If you're allowed to use Apache Commons then this is what you're looking for:

StringUtils.replaceEach(String text, String[] searchList, String[] replacementList)

you can find more examples in the documentation here.

Comments

1

This one should work for you

precio_gold = precio_gold.replaceAll("[$€]", "");

Comments

1

You can always try with

precio_gold = precio_gold.replace(",", ".").replaceAll("\\$|€", "");

or even

precio_gold = "59,99$".replace(",", ".").replaceAll("\\$|€", "");

Comments

0

You wanted to know if there are more possiblities in the first argument. I say, you can use Ternary operator in the first argument to pick and choose conditionally what exactly you want to change.

Here's a brief example:

    String precio_gold = "FFF#vvty^kk^k#hhh9";
    precio_gold = precio_gold.replace(precio_gold.indexOf("#") ==3 ? "#": "^", "0");
    // precio_gold = precio_gold.replace("$", "");
    System.out.println(precio_gold);

This code checks if the "#" is at the 3rd index and if only its at 3rd index, it gets replaced with 0, else the ^ gets replaced with 0.

You can keep adding to the conditions after the : in the ternary operator.

Nice one lined approach. Hope it helps!

Comments

Your Answer

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.