繁体   English   中英

C#,一种优雅的方式来拆分字符串,首先(也是第一次)出现两个或多个空格?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM