4
$\begingroup$

The Problem: I often paste strings into Mathca which have many what I call "extender characters" --which may extend the character set." We know these have to be dealt with so Mathca can deal with the string without error.

Here is an example of such a string. The "extender characters" are the \ characters. (Happens to be a directory string pasted from W7.)

myString=
"C:\Users\NEW USER\Documents\Data Files General\Creative Write\Analysis incl Econ Trips etc\Write Econ Soc Chce Fin Trips\Mathca\Learn and How Tos\Projcts Apps Unoptimized\Gradebook12x3x12plus.nb"

Here is code which converts myString into something more clear to Mathca:

    FromCharacterCode[
       Insert[ToCharacterCode[
          myString],92,
          Position[ToCharacterCode[myString],92]]]  (* 92 is the ascii code of \ *)

The code works It replaces all "\" by "\\".
But it generates many "Unknown string escape" errors (warning that the \ characters in the string enable the extended character set, etc.)

There needs to be (there must be!) a function which permits us to optionally suppress the control character potentiality of certain characters. Then I would simply enter:

ConstrainedString[myString,{list of characters to optionally constrain}]

Then I could use

ReplaceAll[myConstrainedString, "\"->"\\"]

I don't want to be blindsided by extender characters not relevant in my code if, in that code, those characters only have the literal meaning. I want a clean way of converting or manipulating strings which have "extender characters", so a longer list of functions will be available.

$\endgroup$
3
  • $\begingroup$ Shouldn't Mathematica offers to escape the pasted string for you? Like this. $\endgroup$
    – Silvia
    Jan 1, 2014 at 3:26
  • $\begingroup$ @Silvia well that only works on a front-end, and not if working directly in the kernel or on Wolfram Engine. In the kernel StringReplace["\a\b\c", "\\" -> "\\\\"] complains that these are Syntax::stresc: Unknown string escape. $\endgroup$
    – rhermans
    May 5, 2022 at 14:23
  • $\begingroup$ @rhermans I would argue "\a\b\c" is a syntax error at the beginning. Or maybe it boils down to whose responsibility it is to differentiate "\a" (two characters) and "\n" (one character): should kernel tries to be smart or should users differentiate them when construct the string? $\endgroup$
    – Silvia
    May 8, 2022 at 8:11

1 Answer 1

2
$\begingroup$

A somewhat simpler method would be to use StringReplace to change the single backslash \ into double backslashes \\. Each needs to be "escaped" (doubled) in order to work.

StringReplace[myString, "\\" -> "\\\\"]

It's not clear if this solves the "Unknown string escape" errors.

$\endgroup$
2
  • $\begingroup$ Why does each need to be escaped (doubled). I never thought of that... $\endgroup$
    – user11515
    Jan 1, 2014 at 8:06
  • $\begingroup$ The backslash indicates that the next character is special. So to get an actual backslash inside the quote, you have to indicate that the backslash is actually a backslash (and not the special escape character), which is done by doubel-backslash. This is a particular example of an escape character en.wikipedia.org/wiki/Escape_character $\endgroup$
    – bill s
    Jan 1, 2014 at 15:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.