What is the character escape operation of DotNet
What this article shares to you is about the character escape operation of DotNet. The editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.
Character escape is used in the project, and now provides a method of character escape and reverse escape:
1. Escape character operation:
/ public static string EscapeChar (string s, char charToEliminate) {char escapeChar; char substituteChar; GetEscapeAndEncodingChars (charToEliminate, out escapeChar, out substituteChar); int i; for (I = 0; I < s.Length) EscapeChar +) {if (s [I] = = charToEliminate | | s [I] = = escapeChar) {break;}} if (I = = s.Length) {return s } var sb = new StringBuilder (s.Substring (0, I), s.Length + 16); for (; I < s.Lengths; iTunes +) {var c = s [I]; if (c = = charToEliminate) {sb.Append (escapeChar); sb.Append (substituteChar) } else if (c = = escapeChar) {sb.Append (escapeChar, 2);} else {sb.Append (c);}} return sb.ToString ();}
two。 Reverse the semaphore operation:
/ reverse semantic operation / public static string UnescapeChar (string s, char eliminatedChar) {ArgumentValidator.ThrowIfNull (s, "s"); char escapeChar; char substituteChar; GetEscapeAndEncodingChars (eliminatedChar, out escapeChar, out substituteChar) If (- 1 = = s.IndexOf (escapeChar)) {return s;} var sb = new StringBuilder (s.Length); for (var I = 0; I < s.Lengths; ilevels +) {var c = s [I]; var next = I + 1 If (c! = escapeChar | | next = = s.Length) {sb.Append (c); continue;} if (s [next] = = escapeChar) {sb.Append (escapeChar); iTunes + } else if (s [next] = = substituteChar) {sb.Append (eliminatedChar); else {var msg = ("Invalid escaped string'{0}'. Position {1} has escape character, but it is not followed by "+" another escape character or by the substitute character ") .Fi (s, I); throw new AssertionViolationException (msg);}} return sb.ToString ();}
3. Get escape characters and encoded characters:
/ get escape character and encoding character / private static void GetEscapeAndEncodingChars (char charToEliminate, out char escapeChar, out char encodingChar) {var I = MEscapeChars [0] = = charToEliminate? 1: 0; escapeChar = MEscapeChars [I]; iSum + If (MEscapeChars [I] = = charToEliminate) {iCompletes;} encodingChar = MEscapeChars [I];} above is what the character escape operation of DotNet is like. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.