使用c#基于空格分隔字符串中的单词

时间:2016-09-09 07:23:21

标签: c# string

我有这样的字符串:

string mystring = "12353    90123B41094     A01283410294    3"

我需要将具有3或4个字符串的字符串分隔开来。

这是我的尝试:

   string block = "";
Arraytext = text.ToCharArray();
            for(int i = 0; i <= text.Length; i++)
            {
                while (Arraytext[i] !=' ') { block = block + Arraytext[i]; counter++; } // also tried Arraytext[i] != '/0'
            }
while (Arraytext [counter] == ' ')counter++; //to get where the next string begins
//repeat this function until the strings has been obtained

这不起作用:

  • 字符串块填充0
  • Arraytext没有检测到空格,因此循环运行整个字符串。我尝试过' ''/0'

1 个答案:

答案 0 :(得分:4)

分隔您可以使用的空格之间的单词

string mystring = "12353    90123B41094     A01283410294    3";
string[] result = mystring.Split(new []{' '},  StringSplitOptions.RemoveEmptyEntries); 
相关问题