Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

How does one conveniently convert a string like:

"Ya no me burlar\:00e9 de la diabetes de Nick Jonas. Me lo guardar\:00e9 para cuando le corten la pierna. S\:00ed es que a\:00fan alguien lo recuerda, claro."

to

"Ya no me burlaré de la diabetes de Nick Jonas. Me lo guardaré para cuando le corten la pierna. Sí es que aún alguien lo recuerda, claro."?

Certainly if I directly input the former, Mathematica autoformats it to the latter. If that text, however, comes to me whole from an outside source it is not automatically reformatted to show the special character. There is the ShowSpecialCharacters option in TextCell, for example, but that doesn't preserve the object as a String.

I intend to parse a lot of text and would ideally prefer to avoid applying a function that first finds all substrings "\:", then converts to CharacterCode, then StringReplace's, etc.

share|improve this question
1  
A strange choice of example phrase, I must say. Anyway, could you give more detail on how the text "comes to you whole from an outside source"? Do you mean these escape sequences appear literally in the data, or that these are Unicode strings that are escaped by Mathematica when read in? –  Oleksandr R. May 31 '13 at 23:47
    
What is the problem you have with the StringReplace route? –  Sjoerd C. de Vries Jun 1 '13 at 21:22
    
It seems inelegant and unnecessarily slow. It's clear that there is an existing framework for showing special characters, i.e. ShowSpecialCharacters, and I wanted to use an inbuilt function to do it. –  hailekofi Jun 3 '13 at 17:53

1 Answer 1

up vote 4 down vote accepted

I put your example sentence with Unicode codes in a text file ("C:\Users\Sjoerd\Desktop\test.txt"). A simple Import yields

st1= Import["C:\\Users\\Sjoerd\\Desktop\\test.txt", "Text"]

"Ya no me burlar\:00e9 de la diabetes de Nick Jonas. Me lo guardar\:00e9 para cuando le corten la pierna. S\:00ed es que a\:00fan alguien lo recuerda, claro."

You can use ToExpression to turn this into a string with the characters changed to their latin equivalents by adding escaped double quotes before and after:

st2 = ToExpression["\"" <> Import["C:\\Users\\Sjoerd\\Desktop\\test.txt", "Text"] <>  "\""]

"Ya no me burlaré de la diabetes de Nick Jonas. Me lo guardaré para \ cuando le corten la pierna. Sí es que aún alguien lo recuerda, claro."

Taking a piece of the original import at the position of the original special code yields:

StringTake[st1, 15 + {1, 6}]

"\:00e9"

Doing this for the second string now shows that it is really turned into a single code:

StringTake[st2, 15 + {1, 6}]

"é de l"

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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