views:

54

answers:

1

I have the following string

"Messatsu Gou Hadou (滅殺豪波動)"

Is there a way to escape these characters so it would be converted to

"滅殺豪波動"

Is there some way to do it?

+3  A: 

You could write a function like this:

public static string EscapeString(string s)
{
    StringBuilder sb = new StringBuilder();

    foreach (char c in s)
    {
                int i = (int)c;
                if (i < 32 || i > 126)
                {
                    sb.AppendFormat("&#{0};", i);
                }
                else
                {
                    sb.Append(c);
                }

    }

    return sb.ToString();
}
Clicktricity
This is close but you need to use `"{0};"` instead of `"\\u{0:X04}"` to match the OP's format. I deleted my answer since it was similar.
Ahmad Mageed
Also, 127 is a control character (DELETE), so you probably want the upper bound on direct characters to be 126.
dan04
Thanks for the updates - example updated
Clicktricity