[英]C#, an elegant way to split up string at first (and only first) occurence of two or more whitespaces?
有沒有一種優雅的方法可以在第一次(也是第一次)出現兩個或多個空格時拆分字符串? 或者至少找到這兩個或更多空白字符串的索引。
非常感謝。
您可以構造一個實例而不是使用 static 方法並利用限制執行拆分次數的重載:
Regex regex = new Regex(@"\s{2,}");
string[] result = regex.Split(input, 2); // only 1 split, two parts
看看這個: String.Split only on first separator in C#?
或: http://msdn.microsoft.com/en-us/library/c1bs0eda.aspx
String.Split(分隔符,要返回的字符串數)
使用正則表達式拆分,如此處所示
我猜你最終會得到這樣的結果:
RegexOptions options = RegexOptions.None;
Regex regex = new Regex(@"[ ]{2,}", options);
string[] operands = Regex.Split(operation, regex);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.