日期:2014-05-18  浏览次数:20772 次

C#,解析字符串的问题
问下各位,在C# 中用代码解析以下字符串怎么做最简单:
输入字符串: our address is: 9 (2): 86-91
输出字符串: our address is district 9, no. 2, block 86-91
其中的几个数字是动态的。

------解决方案--------------------
数字用正则?
------解决方案--------------------
C# code
        string s = "our address is: 9 (2): 86-91";
        Match match = Regex.Match(s, @"our address is:\s*(\d+)\s*\((\d+)\):\s*([0-9\-]+)");
        string r = string.Format(@"our address is district {0}, no. {1}, block {2}", match.Groups[1].Value, match.Groups[2].Value, match.Groups[3].Value);
        Console.Write(r);
        Console.ReadKey();