Convert a Unicode string to an escaped ASCII string

前端 未结 9 1419
广开言路
广开言路 2020-11-22 04:00

How can I convert this string:

This string contains the Unicode character Pi(π)

into an escaped A

9条回答
  •  旧时难觅i
    2020-11-22 04:39

    This goes back and forth to and from the \uXXXX format.

    class Program {
        static void Main( string[] args ) {
            string unicodeString = "This function contains a unicode character pi (\u03a0)";
    
            Console.WriteLine( unicodeString );
    
            string encoded = EncodeNonAsciiCharacters(unicodeString);
            Console.WriteLine( encoded );
    
            string decoded = DecodeEncodedNonAsciiCharacters( encoded );
            Console.WriteLine( decoded );
        }
    
        static string EncodeNonAsciiCharacters( string value ) {
            StringBuilder sb = new StringBuilder();
            foreach( char c in value ) {
                if( c > 127 ) {
                    // This character is too big for ASCII
                    string encodedValue = "\\u" + ((int) c).ToString( "x4" );
                    sb.Append( encodedValue );
                }
                else {
                    sb.Append( c );
                }
            }
            return sb.ToString();
        }
    
        static string DecodeEncodedNonAsciiCharacters( string value ) {
            return Regex.Replace(
                value,
                @"\\u(?[a-zA-Z0-9]{4})",
                m => {
                    return ((char) int.Parse( m.Groups["Value"].Value, NumberStyles.HexNumber )).ToString();
                } );
        }
    }
    

    Outputs:

    This function contains a unicode character pi (π)

    This function contains a unicode character pi (\u03a0)

    This function contains a unicode character pi (π)

提交回复
热议问题