Linux 拨号vps windows公众号手机端

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

lewis 8年前 (2017-05-05) 阅读数 10 #程序编程
文章标签 c#split

您可以使用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方法。这样,连续的多个空字符串将被过滤掉,只返回非空的子字符串。

版权声明

本文仅代表作者观点,不代表米安网络立场。

发表评论:

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

热门