c#怎么用split过滤多个空字符串

lewis 2017-05-05 31次阅读

您可以使用StringSplitOptions.RemoveEmptyEntries参数来过滤多个空字符串。以下是使用split方法过滤多个空字符串的示例代码:

string input = "Hello,,World,,,!";
char[] separators = new char[] { ',' };
string[] result = input.Split(separators, StringSplitOptions.RemoveEmptyEntries);

foreach (string word in result)
{
    Console.WriteLine(word);
}

输出结果为:

Hello
World
!

在上述示例中,我们使用逗号作为分隔符,并将StringSplitOptions.RemoveEmptyEntries参数传递给Split方法。这样,连续的多个空字符串将被过滤掉,只返回非空的子字符串。



发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。