- Home /
Write a Unicode escape character code to a text file
I've got C# script that converts a text file with all the game narrative in from the writer and converts it into a string file that can be used in the game. I need to put the Unicode escape characters into the string file by their Unicode codes - so, I need to swap all apostrophes for \u0027 and so on. The text file written out however swaps the Unicode codes back to the character I'm trying to replace - so:
string cleanString = dirtyString.Replace("'","\u0027");
Results in the word "There's" being written out as "There's", rather than "There\u0027s".
To test I tried putting in another word instead of a code and that worked fine -
string cleanString = dirtyString.Replace("'","TEST");
Results in "ThereTESTs" so I'm thinking that its the Write process that's swapping the code back to the character. I'm using SystemIO File.WriteAllText to write the text file.
I don't know if I'm right here, but the problem could be that you are presenting it as a string rather than a character.
https://msdn.microsoft.com/en-us/library/aa664669%28v=vs.71%29.aspx
http://stackoverflow.com/questions/9738282/replace-unicode-escape-sequences-in-a-string
Looking at maccabbe's answer, I think I misread.
Answer by maccabbe · Mar 03, 2015 at 01:38 PM
This is because "'" == "\u0027"
. If you want "\u0027" to show up in your text file, you have to escape the escape sequence, i.e.
string cleanString = dirtyString.Replace("'","\\u0027");
Cool - That results in \\u0027 in the string file ins$$anonymous$$d of the apostrophes. I'm not sure if this is now going to work to create an apostrophe when it is read in as a string file - I'll give it a whirl and see what happens. Ta!
Your answer
Follow this Question
Related Questions
Finding a character unicode in C# 0 Answers
Read dialog text from text file in javascript 0 Answers
Set string.ToCharArray() to the first letter 1 Answer
X.Text just uses random values 1 Answer