Search code examples
java

how to fix invalid escape sequence


I'm new to Java programming.

I have below string which has one variable in that

("{\r\n" + "  \"tenant\": \"https://example.com/login\"\r\n" + "}")

instead of example.com/login, I need to put variable url in that, but I don't know how to format the string with proper escaping.

Below is the error

Invalid escape sequence (valid ones are \b \t \n \f \r \" \' \\ )

I have gone through many posts still no luck.

"{\r\n :" "\tenant\:" + url + "\r\n" + "}"")

Above is not working. How can I fix this? url is the variable which contains the string example.com.


Solution

  • Like this:

    ("{\r\n" + "  \"tenant\": \"" + url + "\"\r\n" + "}")