■ 이스케이프 유니코드 문자열을 구하는 방법을 보여준다.
▶ Program.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
using System; using System.Globalization; using System.Text; using System.Text.RegularExpressions; namespace TestProject { /// <summary> /// 프로그램 /// </summary> class Program { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 이스케이프 유니코드 문자열 구하기 - GetEscapedUnicodeString(sourceString) /// <summary> /// 이스케이프 유니코드 문자열 구하기 /// </summary> /// <param name="sourceString">소스 문자열</param> /// <returns>이스케이프 유니코드 문자열</returns> private static string GetEscapedUnicodeString(string sourceString) { StringBuilder stringBuilder = new StringBuilder(); foreach(char sourceCharacter in sourceString) { if(sourceCharacter > 127) { string encodedValue = "\\u" + ((int)sourceCharacter).ToString("x4"); stringBuilder.Append(encodedValue); } else { stringBuilder.Append(sourceCharacter); } } return stringBuilder.ToString(); } #endregion #region 비 이스케이프 유니코드 문자열 구하기 - GetNonEscapedUnicodeString(sourceEscapedUnicodeString) /// <summary> /// 비 이스케이프 유니코드 문자열 구하기 /// </summary> /// <param name="sourceEscapedUnicodeString">소스 이스케이프 유니코드 문자열</param> /// <returns>비 이스케이프 유니코드 문자열</returns> private static string GetNonEscapedUnicodeString(string sourceEscapedUnicodeString) { return Regex.Replace ( sourceEscapedUnicodeString, @"\\u(?<Value>[a-zA-Z0-9]{4})", m => { return ((char)int.Parse(m.Groups["Value"].Value, NumberStyles.HexNumber)).ToString(); } ); } #endregion #region 프로그램 시작하기 - Main() /// <summary> /// 프로그램 시작하기 /// </summary> private static void Main() { string source = "\u03a0"; Console.WriteLine($"소스 문자열 : {source}"); string escapedUncodeString = GetEscapedUnicodeString(source); Console.WriteLine($"이스케이프 유니코드 문자열 : {escapedUncodeString}"); string nonEscapedUnicodeString = GetNonEscapedUnicodeString(escapedUncodeString); Console.WriteLine($"유니코드 문자열 : {nonEscapedUnicodeString}"); } #endregion } } |