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();
}
}
}
如果你只談論在開頭和結尾空格,然後使用String.Trim():'海峽= str.Trim();'。否則請參閱上面的重複內容.... –
@OndrejJanacek它非常相似,但不包括字符串開頭和結尾的空格 – evhen14