0

I have a

String url = "/rest/devices/{{deviceId}}";

in which {{deviceId}} value i want to replace with some deviceId.

url.replace("{{deviceId}}", device.getId());

But it's not working.

Therefore, What to do?

anij
  • 1,322
  • 5
  • 23
  • 39
  • Have you tried escaping the braces (e.g. `"\\{\\{deviceId\\}\\}"`)? – Kenneth K. Nov 02 '16 at 20:32
  • @anij, please explain what you mean with _"it's not working"_. – Alex Shesterov Nov 02 '16 at 20:32
  • @KennethK. `String.replace` is NOT regex-based, so no need to escape. – Alex Shesterov Nov 02 '16 at 20:33
  • @Alex when I'm using `.replace()` its not replacing the value, and while using `replaceAll()`, then getting the following exception. `java.util.regex.PatternSyntaxException: Illegal repetition {{deviceId}}` – anij Nov 02 '16 at 20:35
  • 2
    please see the answer by @maszter, it's very likely that this is the reason. The method call itself is correct, but the method **does not modify the original string** — it returns a copy of the string with the replacement done. You may be just discarding the return value, thus the error. – Alex Shesterov Nov 02 '16 at 20:38

1 Answers1

4

I guess you are missing an assignment and result of

url.replace("{{deviceId}}", device.getId()); is ignored.

What about:

url = url.replace("{{deviceId}}", device.getId());

maszter
  • 3,680
  • 6
  • 37
  • 53