当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - How to split string with 1-many spaces and place words in an array

问题描述:

As the title says,

How do i split a string with 1-* numbers of spaces, and place each word in an array

right now I am using Split(' ') it works with single spaces, but when it comes to multiple spaces it causes an issue

Here is my sample string:

0x886fe248 ElanTPCfg.exe 1132 2492 0 -------- 1 0 2014-01-20 09:31:10 2014-01-20 09:31:10

In this example I was only able to get the hexidecimal digit and the executable name, while the others are not read at all

网友答案:
void Main()
{
    string s = "0x886fe248 ElanTPCfg.exe          1132   2492      0 --------      1      0 2014-01-20 09:31:10  2014-01-20 09:31:10";
    Console.WriteLine (s.Split(new[]{' '}, StringSplitOptions.RemoveEmptyEntries));
}

Produces:

网友答案:

You can use StringSplitOptions.RemoveEmptyEntries

var output = input.Split(new [] { " " }, StringSplitOptions.RemoveEmptyEntries);
网友答案:

You can also use Regex.Split:

string s = @"x886fe248 ElanTPCfg.exe          1132   2492      0 --------      1      0 2014-01-20 09:31:10  2014-01-20 09:31:10";
string[] strArr = Regex.Split(s, @"\s+");

\s+ is one or more whitespace character(s).

分享给朋友:
您可能感兴趣的文章:
随机阅读: