On 05/12/2005 11:42, Jon Jagger wrote:
But I want to cater for any verbatim string that would be legal in its non verbatim form. For example, "\xabcd" is a legal non-verbatim string and it creates a string containing a single 16 bit character whose int code is abcd in hex. So I'm after a method where the input would be @"\xabcd" (6 chars long) and it would give me back a string containing a single character whose int code was abcd in hex. The problem is the escape code syntax is somewhat language specific so I'm probably out of luck.
You could achieve the above (and in addition the \n matching you've specified already) with this code: using System; using System.Globalization; using System.Text.RegularExpressions; public class StringUtilities { private static readonly Regex reVerbStr = new Regex(@"\\(n|x[0-9a-f]{4})"); public static string VerbParse(string verbStr) { return reVerbStr.Replace(verbStr, new MatchEvaluator(verbStr_MatchEvaluator)); } private static string verbStr_MatchEvaluator(Match m) { string grp1 = m.Groups[1].ToString(); if (grp1 == "n") { return "\n"; } else if (grp1.StartsWith("x")) { int v = Int32.Parse(grp1.Substring(1), NumberStyles.HexNumber); return new string((char)v, 1); } return ""; } } I'd use that as a starting point and build up all the other control characters \r, etc. You also need to decide how to handle badly formatted strings (FormatException?). Barnaby =================================== This list is hosted by DevelopMentorĀ® http://www.develop.com View archives and manage your subscription(s) at http://discuss.develop.com