Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用两个连续空格拆分字符串_C#_String_Split_Space - Fatal编程技术网

C# 如何使用两个连续空格拆分字符串

C# 如何使用两个连续空格拆分字符串,c#,string,split,space,C#,String,Split,Space,我有一个由连续空格组成的字符串,比如 a(double space)b c d (Double space) e f g h (double space) i 分裂 a b c d e f g h i 目前我正在这样努力 Regex r = new Regex(" +"); string[] splitString = r.Split(strt); 您可以使用: 你可以使用这个方法 返回包含此字符串中的子字符串的字符串数组 由指定字符串数组的元素分隔的。A. 参数指定

我有一个由连续空格组成的字符串,比如

a(double space)b c d (Double space) e f g h (double space) i
分裂

a
b c d
e f g h
i
目前我正在这样努力

   Regex r = new Regex(" +");
        string[] splitString = r.Split(strt);
您可以使用:

你可以使用这个方法

返回包含此字符串中的子字符串的字符串数组 由指定字符串数组的元素分隔的。A. 参数指定是否返回空数组元素

输出将是

a
b c d
e f g h
i

这里使用正则表达式是一个优雅的解决方案

string[] match = Regex.Split("a  b c d  e f g h  i", @"/\s{2,}/",  RegexOptions.IgnoreCase);

另一种方法是使用正则表达式,它允许您将任何空格拆分为两个字符:

string s = "a      b c d   e f g h      \t\t i";
var test = Regex.Split(s, @"\s{2,}");

Console.WriteLine(test[0]); // a
Console.WriteLine(test[1]); // b c d
Console.WriteLine(test[2]); // e f g h
Console.WriteLine(test[3]); // i

您是否尝试过Split(“”?@bobek,但不起作用。Split(“Doublespace”)?@sgud也不起作用(甚至无法编译)。对于您正在尝试的代码,结果是什么?为什么不起作用?发生了什么?这是如何分割字符串的?我知道
Regex.Match
是如何工作的。我认为它没有达到OP的要求<代码>-1来自我。感谢您提供的正则表达式示例。我在拆分电话号码时遇到了问题,将电话号码按单个空格拆分为it代码和号码。数据来自API、JSON。斯普利特不想工作,不知怎的把一个空格读成了两个。我使用了正则表达式,它使用字符串[]arr=regex.Split(数字,@“\s{1,}”);不知道为什么。编号为“+38112085917”。这是否只是为了在拆分前检查而写的响应。当我使用字符串[]arr=“+381 112085917”时,拆分(“”);成功了。仅当使用字符串[]arr=numbers.Split(“”)时;它将以3个数组编号结束,arr[1]为空
string[] match = Regex.Split("a  b c d  e f g h  i", @"/\s{2,}/",  RegexOptions.IgnoreCase);
string s = "a  b c d  e f g h  i";
var test = s.Split(new String[] { "  " }, StringSplitOptions.RemoveEmptyEntries);

Console.WriteLine(test[0]); // a
Console.WriteLine(test[1]); // b c d
Console.WriteLine(test[2]); // e f g h
Console.WriteLine(test[3]); // i
string s = "a      b c d   e f g h      \t\t i";
var test = Regex.Split(s, @"\s{2,}");

Console.WriteLine(test[0]); // a
Console.WriteLine(test[1]); // b c d
Console.WriteLine(test[2]); // e f g h
Console.WriteLine(test[3]); // i