C# 使用 Split() 时执行 Trim()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1977340/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 22:20:45  来源:igfitidea点击:

Perform Trim() while using Split()

c#stringtrim

提问by citronas

today I was wondering if there is a better solution perform the following code sample.

今天我想知道是否有更好的解决方案执行以下代码示例。

string keyword = " abc, foo  ,     bar";
string match = "foo";
string[] split= keyword.Split(new char[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries);
foreach(string s in split)
{
  if(s.Trim() == match){// asjdklasd; break;}
}

Is there a way to perform trim() without manually iterating through each item? I'm looking for something like 'split by the following chars and automatically trim each result'.

有没有办法在不手动遍历每个项目的情况下执行 trim() ?我正在寻找诸如“按以下字符拆分并自动修剪每个结果”之类的东西。

Ah, immediatly before posting I found

啊,在发帖之前我发现了

List<string> parts = line.Split(';').Select(p => p.Trim()).ToList();

in How can I split and trim a string into parts all on one line?

如何将一个字符串拆分和修剪成一行中的所有部分?

Still I'm curious: Might there be a better solution to this? (Or would the compiler probably convert them to the same code output as the Linq-Operation?)

我仍然很好奇:可能有更好的解决方案吗?(或者编译器可能会将它们转换为与 Linq-Operation 相同的代码输出?)

采纳答案by Marc Gravell

Another possible option (that avoids LINQ, for better or worse):

另一个可能的选择(避免 LINQ,无论好坏):

string line = " abc, foo  ,     bar";
string[] parts= Array.ConvertAll(line.Split(','), p => p.Trim());

However, if you just need to know if it is there - perhaps short-circuit?

但是,如果您只需要知道它是否存在 - 也许是短路?

bool contains = line.Split(',').Any(p => p.Trim() == match);

回答by MikeW

I would suggest using regular expressions on the original string, looking for the pattern "any number of spaces followed by one of your delimiters followed by one or more spaces" and remove those spaces. Then split.

我建议在原始字符串上使用正则表达式,寻找模式“任意数量的空格后跟一个分隔符,后跟一个或多个空格”并删除这些空格。然后分裂。

回答by McAden

You're going to find a lot of different methods of doing this and the performance change and accuracy isn't going to be readily apparent. I'd recommend plugging them all into a testing suite like NUnit in order both to find which one comes out on top AND which ones are accurate.

您将找到许多不同的方法来执行此操作,并且性能变化和准确性不会很明显。我建议将它们全部插入到 NUnit 之类的测试套件中,以便找出哪个排在最前面,哪些是准确的。

Use small, medium, and large amounts of text in loops to examine the various situations.

在循环中使用小、中和大量文本来检查各种情况。

回答by Rubens Farias

Try this:

尝试这个:

string keyword = " abc, foo  ,     bar";
string match = "foo";
string[] split = Regex.Split(keyword.Trim(), @"\s*[,;]\s*");
if (split.Contains(match))
{
    // do stuff
}

回答by Jens Granlund

If spaces just surrounds the words in the comma separated string this will work:

如果空格只是围绕逗号分隔字符串中的单词,这将起作用:

var keyword = " abc, foo  ,     bar";
var array = keyword.Replace(" ", "").Split(',');
if (array.Contains("foo"))
{
    Debug.Print("Match");
}

回答by Darius Kucinskas

var parts = line.Split(';').Select(p => p.Trim()).Where(p => !string.IsNullOrWhiteSpace(p)).ToArray();