在 String.Split 操作中指定空格的最佳方法

发布于 2024-11-09 06:47:24 字数 279 浏览 0 评论 0原文

我根据空格分割字符串,如下所示:

string myStr = "The quick brown fox jumps over the lazy dog";

char[] whitespace = new char[] { ' ', '\t' };
string[] ssizes = myStr.Split(whitespace);

在我想要执行此操作的代码中到处定义 char[] 数组是很烦人的。是否有更有效的方法不需要创建字符数组(如果复制到不同的地方很容易出错)?

I am splitting a string based on whitespace as follows:

string myStr = "The quick brown fox jumps over the lazy dog";

char[] whitespace = new char[] { ' ', '\t' };
string[] ssizes = myStr.Split(whitespace);

It's irksome to define the char[] array everywhere in my code I want to do this. Is there more efficent way that doesn't require the creation of the character array (which is prone to error if copied in different places)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

朦胧时间 2024-11-16 06:47:25

您可以这样做:

string myStr = "The quick brown fox jumps over the lazy dog";
string[] ssizes = myStr.Split(' ');

MSDN 有更多示例和参考:

http://msdn.microsoft .com/en-us/library/b873y76a.aspx

You can just do:

string myStr = "The quick brown fox jumps over the lazy dog";
string[] ssizes = myStr.Split(' ');

MSDN has more examples and references:

http://msdn.microsoft.com/en-us/library/b873y76a.aspx

无力看清 2024-11-16 06:47:24

如果您只调用:

string[] ssize = myStr.Split(null); //Or myStr.Split()

或:,

string[] ssize = myStr.Split(new char[0]);

则假定空格为分割字符。来自 string.Split(char[]) 方法的文档页面

如果分隔符参数为 null 或不包含任何字符,则假定空白字符为分隔符。空白字符由 Unicode 标准定义,如果传递到 Char.IsWhiteSpace 方法。

始终、始终、始终阅读文档!

If you just call:

string[] ssize = myStr.Split(null); //Or myStr.Split()

or:

string[] ssize = myStr.Split(new char[0]);

then white-space is assumed to be the splitting character. From the string.Split(char[]) method's documentation page.

If the separator parameter is null or contains no characters, white-space characters are assumed to be the delimiters. White-space characters are defined by the Unicode standard and return true if they are passed to the Char.IsWhiteSpace method.

Always, always, always read the documentation!

别念他 2024-11-16 06:47:24

是的,这里还需要一个答案!

到目前为止,所有解决方案都解决了相当有限的规范输入领域,即:单个空白字符之间元素(尽管向@cherno致敬,至少提到了这个问题)。
但我认为,除了最模糊的场景之外,拆分所有这些应该会产生相同的结果:

string myStrA = "The quick brown fox jumps over the lazy dog";
string myStrB = "The  quick  brown  fox  jumps  over  the  lazy  dog";
string myStrC = "The quick brown fox      jumps over the lazy dog";
string myStrD = "   The quick brown fox jumps over the lazy dog";

String.Split(在此处的其他答案中显示的任何风格中) ) 根本无法正常工作,除非您将 RemoveEmptyEntries 选项附加到以下任一选项:

myStr.Split(new char[0], StringSplitOptions.RemoveEmptyEntries)
myStr.Split(new char[] {' ','\t'}, StringSplitOptions.RemoveEmptyEntries)

如图所示,省略该选项会产生四种不同的结果(标记为 A、B、C 和 D)。所有四个的单一结果使用 RemoveEmptyEntries 时的输入:

String.Split vs Regex.Split

当然,如果您不这样做不喜欢使用选项,只需使用正则表达式替代:-)

Regex.Split(myStr, @"\s+").Where(s => s != string.Empty)

Yes, There is need for one more answer here!

All the solutions thus far address the rather limited domain of canonical input, to wit: a single whitespace character between elements (though tip of the hat to @cherno for at least mentioning the problem).
But I submit that in all but the most obscure scenarios, splitting all of these should yield identical results:

string myStrA = "The quick brown fox jumps over the lazy dog";
string myStrB = "The  quick  brown  fox  jumps  over  the  lazy  dog";
string myStrC = "The quick brown fox      jumps over the lazy dog";
string myStrD = "   The quick brown fox jumps over the lazy dog";

String.Split (in any of the flavors shown throughout the other answers here) simply does not work well unless you attach the RemoveEmptyEntries option with either of these:

myStr.Split(new char[0], StringSplitOptions.RemoveEmptyEntries)
myStr.Split(new char[] {' ','\t'}, StringSplitOptions.RemoveEmptyEntries)

As the illustration reveals, omitting the option yields four different results (labeled A, B, C, and D) vs. the single result from all four inputs when you use RemoveEmptyEntries:

String.Split vs Regex.Split

Of course, if you don't like using options, just use the regex alternative :-)

Regex.Split(myStr, @"\s+").Where(s => s != string.Empty)
你是我的挚爱i 2024-11-16 06:47:24

根据文档

如果分隔符参数为空或不包含任何字符,则假定空白字符为分隔符。空白字符由 Unicode 标准定义,如果将其传递给 Char.IsWhiteSpace 方法,则返回 true。

因此,只需调用 myStr.Split(); 无需传递任何内容,因为 separator 是一个 params 数组。

According to the documentation :

If the separator parameter is null or contains no characters, white-space characters are assumed to be the delimiters. White-space characters are defined by the Unicode standard and return true if they are passed to the Char.IsWhiteSpace method.

So just call myStr.Split(); There's no need to pass in anything because separator is a params array.

素食主义者 2024-11-16 06:47:24

为什么不使用?:

string[] ssizes = myStr.Split(' ', '\t');

Why dont you use?:

string[] ssizes = myStr.Split(' ', '\t');
轮廓§ 2024-11-16 06:47:24

请注意,即使使用 String.Split(null) ,相邻的空格也不会被视为单个分隔符。如果您的任何标记由多个空格或制表符分隔,您将在数组中返回空字符串。

从文档中:

分隔符的每个元素定义一个单独的分隔符。如果
两个分隔符相邻,或者在开头找到分隔符
或该实例的末尾,相应的数组元素包含
空。

Note that adjacent whitespace will NOT be treated as a single delimiter, even when using String.Split(null). If any of your tokens are separated with multiple spaces or tabs, you'll get empty strings returned in your array.

From the documentation:

Each element of separator defines a separate delimiter character. If
two delimiters are adjacent, or a delimiter is found at the beginning
or end of this instance, the corresponding array element contains
Empty.

厌味 2024-11-16 06:47:24

所以不要复制和粘贴!提取一个函数来进行拆分并重用它。

public static string[] SplitWhitespace (string input)
{
    char[] whitespace = new char[] { ' ', '\t' };
    return input.Split(whitespace);
}

代码重用是你的朋友。

So don't copy and paste! Extract a function to do your splitting and reuse it.

public static string[] SplitWhitespace (string input)
{
    char[] whitespace = new char[] { ' ', '\t' };
    return input.Split(whitespace);
}

Code reuse is your friend.

迷乱花海 2024-11-16 06:47:24

你可以使用

var FirstString = YourString.Split().First();

分割字符串并在空格之前获取其第一次出现的位置。

You can use

var FirstString = YourString.Split().First();

to split a string and get its first occurrence before the space.

零崎曲识 2024-11-16 06:47:24

你不能内嵌吗?

var sizes = subject.Split(new char[] { ' ', '\t' });

否则,如果您经常执行此操作,则始终可以创建常量或包含该 char 数组的内容。

正如其他人指出的那样,您也可以根据文档使用 null 或空数组。当您这样做时,它将自动使用空白字符。

var sizes = subject.Split(null);

Can't you do it inline?

var sizes = subject.Split(new char[] { ' ', '\t' });

Otherwise, if you do this exact thing often, you could always create constant or something containing that char array.

As others have noted you can according to the documentation also use null or an empty array. When you do that it will use whitespace characters automatically.

var sizes = subject.Split(null);
无声情话 2024-11-16 06:47:24

你为什么不这样做:

var ssizes = myStr.Split(" \t".ToCharArray());

似乎有一个方法 String .NET 4.0 中的 .ToCharArray()

编辑:正如 VMAtm 所指出的,该方法已经存在于.NET 2.0中!

Why don't you just do this:

var ssizes = myStr.Split(" \t".ToCharArray());

It seems there is a method String.ToCharArray() in .NET 4.0!

EDIT: As VMAtm has pointed out, the method already existed in .NET 2.0!

纵情客 2024-11-16 06:47:24

如果出现重复相同代码的问题,请在封装拆分逻辑的 String 类上编写扩展方法。

If repeating the same code is the issue, write an extension method on the String class that encapsulates the splitting logic.

回梦 2024-11-16 06:47:24

在 C# 中,您可以使用下面的代码。

string[] sepratedStrings = s.Split(new Char[] { ' ' });

In C# you can use Below Code.

string[] sepratedStrings = s.Split(new Char[] { ' ' });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文