I have a string: let value = "Emrah Tebrikeder120\'\'\'\'\'\'\'";
. When I try to output it to the console, all the \ are deleted. How do I make all the \ in the string appear?
Asked
Active
Viewed 28 times
0

John
- 33
- 7
-
1https://stackoverflow.com/questions/10041998/how-can-i-use-backslashes-in-a-string answers the question – 35308 Jul 02 '22 at 18:07
2 Answers
1
Use a double-backslash, like this:
let value = "Emrah Tebrikeder120\\'\\'\\'\\'\\'\\'\\'";
The "\'" is considered as escape sequence.

lukasl-dev
- 724
- 3
- 11
-
I need everything in the string to be replaced in one go. How can I do this? – John Jul 02 '22 at 18:11
-
-
"Emrah Tebrikeder120\'\'\'\'\'\'\'" => "Emrah Tebrikeder120\\'\\'\\'\\'\\'\\'\\'" – John Jul 02 '22 at 18:13
-
-
You misunderstand me. I mean I need to make a string "normally" using some string method/cycle/function or something. **this should not be done manually**. A "normal" string is a string in which all \ are replaced by \\. – John Jul 02 '22 at 18:18
-
Ouh, sorry. I suppose that it should work like this: `let replaced = value.replace("\'", "\\\'")` – lukasl-dev Jul 02 '22 at 18:21
0
\
is an escape character so, it will be ignored. If you want to keep it, you will need to escape the escape character (put double \
). Like this:
let value = "Emrah Tebrikeder120\\'\\'\\'\\'\\'\\'\\'";

User456
- 1,216
- 4
- 15