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?

  • $\begingroup$ The "instead returns" should read "y\/G", somehow this site does escaping of text as well. $\endgroup$ – March Ho Apr 4 '13 at 22:24
  • 1
    $\begingroup$ This looks like the expected behavior. Did you try "\\/" as search string? $\endgroup$ – Sjoerd C. de Vries Apr 4 '13 at 22:52
  • $\begingroup$ 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. $\endgroup$ – March Ho Apr 5 '13 at 8:52
  • $\begingroup$ 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. $\endgroup$ – 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 *)
| improve this answer | |
  • $\begingroup$ What if I wanted the string to be interpreted literally (i.e. to return the expected result as asked in the initial question?) $\endgroup$ – March Ho Apr 5 '13 at 5:42
  • $\begingroup$ 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. $\endgroup$ – bill s Apr 5 '13 at 9:19
  • $\begingroup$ @bills just to be pedantic, \n is the line feed; the carriage return is \r... $\endgroup$ – Oleksandr R. Apr 5 '13 at 9:44
  • $\begingroup$ 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. $\endgroup$ – March Ho Apr 5 '13 at 10:13
  • $\begingroup$ @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. $\endgroup$ – 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.

| improve this answer | |
  • $\begingroup$ This is the same answer I wrote, isn't it? ANy reason why you duplicated it? $\endgroup$ – Szabolcs Apr 5 '13 at 13:58
  • $\begingroup$ @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. $\endgroup$ – Oleksandr R. Apr 9 '13 at 6:44
  • $\begingroup$ The following function need this transform to work: StrRep[str_] := With[{}, StringReplace[ToString[str, InputForm], {"," -> "},{", "->" -> ","}]] $\endgroup$ – Frank Jan 9 '18 at 18:54

Not the answer you're looking for? Browse other questions tagged or ask your own question.