Regex - replace spaces
本问题已经有最佳答案,请猛点这里访问。
如何用C_中的一个空格替换字符串中的多个空格?
例子:
1 | PLNICI PERO 2165/HORNET SPACESPACESPACESPACESPACE |
将是:
1 | PLNICI PERO 2165/HORNET |
其次:
例子:
1 | SPACESPACESPACESPACESPACE 77.000 |
将是:
1 | 77.000 |
你知道吗?谢谢
1 2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | using System; using System.Text; using System.Text.RegularExpressions; // for Regex namespace tentitive { class Program { static void Main(string[] args) { string firstString ="PLNICI PERO 2165/HORNET "; string secondString =" 77.000"; string toBeReplaced ="[ ]+"; // pattern to be replaced i.e. one or more occurences of white spaces string replacer =""; // has to be replaced with only One white space string neededFirstString = Regex.Replace(firstString, toBeReplaced, replacer).ToString(); string neededSecondString = Regex.Replace(secondString, toBeReplaced, replacer).ToString(); Console.WriteLine(neededFirstString); Console.WriteLine(neededSecondString); Console.ReadLine(); } } } |