2

how i can convert this word

mamá

to this word

mam\U00E1

2
  • It's not "converting", it is escaping. Commented Jun 14, 2011 at 18:12
  • What is the rule for deciding which character to escape? I mean, why not escape them all? Commented Jun 14, 2011 at 18:55

1 Answer 1

2

You're looking for something like the code below.

StringBuilder sb = new StringBuilder();
string word = "mamá";
foreach (char c in word)
{
    if (' ' <= c && c <= '~')
    {
        sb.Append(c);
    }
    else
    {
        sb.AppendFormat("\\U{0:X4}", (int)c);
    }
}
string escapedWord = sb.ToString();

Or in a more compact way:

Func<char, string> escapeIfNecessary = c => (' ' <= c && c <= '~') ? c.ToString() : string.Format("\\U{0:X4}", (int)c);
escapedWord = string.Join("", word.Select(escapeIfNecessary).ToArray());
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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