-1

enter image description here

How I can fix it?

String replace1 = WEBSITE.replaceAll("{fromNumber}", number);

this character "{" error in replaceAll function. Thank you

mate00
  • 2,727
  • 5
  • 26
  • 34
Tanat29
  • 15
  • 1
  • 4
    Read the javadocs for `replaceAll`. The first argument is a regex, not a simple string. Now read the javadocs for `Pattern` to find out the **syntax** for regexes in Java. Hint: `{` is a regex meta-character, and it needs to be escaped with a `\`. But a `\` in a string literal needs to be escaped too. – Stephen C Jul 28 '19 at 02:48
  • `String replace1 = WEBSITE.replaceAll("\\{fromNumber\\}", String.valueOf(number));`. The curly braces in [RegEx](https://www.vogella.com/tutorials/JavaRegularExpressions/article.html) have a special purpose and therefore must be escaped. Also, you must replace with a string therefore convert **number** to string. – DevilsHnd - 退職した Jul 28 '19 at 04:34

2 Answers2

0

As @Stephen C has already explained replaceall method's first argument is a regex.

Looks you are trying to replace {fromNumber} simple string with a given number. So instead of using replaceall use replace method which accepts a string as a first argument.

String replace1 = WEBSITE.replace("{fromNumber}", number);

lahiruk
  • 433
  • 3
  • 13
0

I is not working because '{' is a regex meta-character and replaceAll is using it as so. If you want to replace all "{fromNumber}" from you String then you have to :

String replace1 = WEBSITE.replaceAll("\{fromNumber\}", number);

But if you just have to replace one then you can go with @lahiruk's answer and use

String replace1 = WEBSITE.replace("{fromNumber}", number);

Something to add here , you can use replace any number of times if you know how many times your String will contain the String to be replaced.


For more info

Syntax of regexp.

String.repaceAll()

Rohit
  • 495
  • 9
  • 16