# String replacement of special characters [duplicate]

The function

StringReplace["y\\/G", {"|<" -> "K", "\/" -> "V", "]" -> "J",
"><" -> "X", "_" -> "", "." -> "", "," -> "", "'" -> ""}]


fails to provide the expected answer "y\VG" and instead returns "y\/G" due to the unusual (to me) way Mathematica handles strings in order to allow for symbols and images in strings.

How would one do a string replace where the symbols are literally replaced, in the order the string is shown?

• The "instead returns" should read "y\/G", somehow this site does escaping of text as well. – March Ho Apr 4 '13 at 22:24
• This looks like the expected behavior. Did you try "\\/" as search string? – Sjoerd C. de Vries Apr 4 '13 at 22:52
• This behaviour is not expected if the string is viewed literally, without escaping characters. I do understand why Mathematica does this, but I would like to know of code which makes it behave in the way I want it to. – March Ho Apr 5 '13 at 8:52
• You can write such code: turn the string into the ascii equivalent, do the substitution on the numbers in the ascii code, then translate back. – bill s Apr 5 '13 at 10:10

You failed to escape the \ character in the string pattern. Try the following:

StringReplace["y\\/G", {"\\/" -> "V"}]

(* ==> yVG *)

• What if I wanted the string to be interpreted literally (i.e. to return the expected result as asked in the initial question?) – March Ho Apr 5 '13 at 5:42
• I think this is a case where your "expected" answer doesn't actually make much sense. The reason for the "escaping" of characters is so that strings can represent more things than just letters. For example, take "carriage return"... there needs to be a way to represent this within in a string: the use of \n is the convention. Hence some character must tell the string parser that the "\n" is not taken literally. In Mathematica it is backslash. Other languages may use different symbols for this, but the procedure is similar. – bill s Apr 5 '13 at 9:19
• @bills just to be pedantic, \n is the line feed; the carriage return is \r... – Oleksandr R. Apr 5 '13 at 9:44
• I understand the need and purpose of escaping, just that it does not give the answer that I want in this case without needing an additional intermediate step. – March Ho Apr 5 '13 at 10:13
• @MarchHo I don't quite understand what you mean by "an intermediate step". IMO, the concept of a "literal interpretation" of a string is not very meaningful: the syntax used to represent the byte sequence given by the ASCII characters \/ as a string is "\\/", just like a string starts and ends with " and not any other characters. To specify a raw byte array as a string, one must use the proper syntax for strings, otherwise one has specified a different (possibly invalid) string relative to what was intended. – Oleksandr R. Apr 5 '13 at 11:43
str = "y\\/G"
str = ToString[str, InputForm]
StringReplace[str, {"|<" -> "K", "\\/" -> "V", "]" -> "J",
"><" -> "X", "_" -> "", "." -> "", "," -> "", "'" -> ""}]


provides the expected result.

• This is the same answer I wrote, isn't it? ANy reason why you duplicated it? – Szabolcs Apr 5 '13 at 13:58
• @Szabolcs ToString[str, InputForm] does insert some additional escapes, but I admit it isn't clear to me why/when this would be helpful. Perhaps there is some wider context that isn't reflected in the question. – Oleksandr R. Apr 9 '13 at 6:44
• The following function need this transform to work: StrRep[str_] := With[{}, StringReplace[ToString[str, InputForm], {"," -> "},{", "->" -> ","}]] – Frank Jan 9 '18 at 18:54