I'm trying the following
我正在尝试以下方式
string tl = " aaa, bbb, ccc, dddd eeeee";
var tags = new List<string>();
tags.AddRange(tl.Split(','));
tags.ForEach(x => x = x.Trim().TrimStart().TrimEnd());
var result = String.Join(",", tags.ToArray());
But it doesn't work, the tags always come back as " aaa", " bbb".
但它不起作用,标签总是以“aaa”,“bbb”的形式返回。
How can I trim all elements in a list?
如何修剪列表中的所有元素?
5 个解决方案
#1
25
// you can omit the final ToArray call if you're using .NET 4
var result = string.Join(",", tl.Split(',').Select(s => s.Trim()).ToArray());
If you only need the final result string, rather than the intermediate collection, then you could use a regular expression to tidy the string. You'll need to benchmark to determine whether or not the regex outperforms the split-trim-join technique:
如果您只需要最终结果字符串而不是中间集合,那么您可以使用正则表达式来整理字符串。您需要进行基准测试以确定正则表达式是否优于split-trim-join技术:
var result = Regex.Replace(tl, @"(?<=^|,) +| +(?=,|$)", "");
#2
7
The reason your approach doesn't work is that the x
is a copy of the current string reference being processed in the ForEach
call (i.e. local to the delegate). Therefore the assignment doesn't affect the item referenced in the list.
您的方法不起作用的原因是x是在ForEach调用中处理的当前字符串引用的副本(即,委托的本地)。因此,分配不会影响列表中引用的项目。
#3
4
What's going on is that you're trying to modify a collection using a foreach
statement- which is a no-no. Collections cannot be modified with a foreach
.
发生的事情是你试图使用foreach语句修改集合 - 这是一个禁忌。使用foreach无法修改集合。
You'll need to modifiy it a for
loop, or, using lambdas, you can use LukeH's solution.
你需要将它修改为for循环,或者使用lambdas,你可以使用LukeH的解决方案。
#4
1
Your problem is that there is no comma between dddd and eeeee. If you want those to be separate, you need to split on ' '
, strip the commas, and then trim extra whitespace.
你的问题是dddd和eeeee之间没有逗号。如果你想要那些是分开的,你需要拆分'',删除逗号,然后修剪额外的空格。
string tl = " aaa, bbb, ccc, dddd eeeee";
var result = t1.Split(' ').Where(s => !String.IsNullOrEmpty())
.Select(s => s.Replace(',','').Trim())
.ToArray();
#5
0
Ran into the same problem. @Lee already explained that Lamda .ForEach() uses a copy.
陷入同样的问题。 @Lee已经解释过Lamda .ForEach()使用了副本。
You can write an Extension Method like this and use a for loop (foreach also not possible):
您可以编写这样的扩展方法并使用for循环(foreach也不可能):
public static class StringListExtensions
{
public static void TrimAll(this List<string> stringList)
{
for (int i = 0; i < stringList.Count; i++)
{
stringList[i] = stringList[i].Trim(); //warning: do not change this to lambda expression (.ForEach() uses a copy)
}
}
}
Use it like this:
像这样用它:
var productNumbers = new List<string>(){ "11111", " 22222 " }
productNumbers.TrimAll();
should result in: List(){ "11111", "22222" }
应该导致:List(){“11111”,“22222”}
I didn't use the split and re-join solution (chosen solution) because there can be a comma inside one of the string items. The regex version is not self explanatory. This is old-school but safer and can be easily understood...
我没有使用拆分和重新加入解决方案(选择的解决方案),因为其中一个字符串项可能有一个逗号。正则表达式版本不是自我解释。这是老派但更安全,可以很容易理解......
#1
25
// you can omit the final ToArray call if you're using .NET 4
var result = string.Join(",", tl.Split(',').Select(s => s.Trim()).ToArray());
If you only need the final result string, rather than the intermediate collection, then you could use a regular expression to tidy the string. You'll need to benchmark to determine whether or not the regex outperforms the split-trim-join technique:
如果您只需要最终结果字符串而不是中间集合,那么您可以使用正则表达式来整理字符串。您需要进行基准测试以确定正则表达式是否优于split-trim-join技术:
var result = Regex.Replace(tl, @"(?<=^|,) +| +(?=,|$)", "");
#2
7
The reason your approach doesn't work is that the x
is a copy of the current string reference being processed in the ForEach
call (i.e. local to the delegate). Therefore the assignment doesn't affect the item referenced in the list.
您的方法不起作用的原因是x是在ForEach调用中处理的当前字符串引用的副本(即,委托的本地)。因此,分配不会影响列表中引用的项目。
#3
4
What's going on is that you're trying to modify a collection using a foreach
statement- which is a no-no. Collections cannot be modified with a foreach
.
发生的事情是你试图使用foreach语句修改集合 - 这是一个禁忌。使用foreach无法修改集合。
You'll need to modifiy it a for
loop, or, using lambdas, you can use LukeH's solution.
你需要将它修改为for循环,或者使用lambdas,你可以使用LukeH的解决方案。
#4
1
Your problem is that there is no comma between dddd and eeeee. If you want those to be separate, you need to split on ' '
, strip the commas, and then trim extra whitespace.
你的问题是dddd和eeeee之间没有逗号。如果你想要那些是分开的,你需要拆分'',删除逗号,然后修剪额外的空格。
string tl = " aaa, bbb, ccc, dddd eeeee";
var result = t1.Split(' ').Where(s => !String.IsNullOrEmpty())
.Select(s => s.Replace(',','').Trim())
.ToArray();
#5
0
Ran into the same problem. @Lee already explained that Lamda .ForEach() uses a copy.
陷入同样的问题。 @Lee已经解释过Lamda .ForEach()使用了副本。
You can write an Extension Method like this and use a for loop (foreach also not possible):
您可以编写这样的扩展方法并使用for循环(foreach也不可能):
public static class StringListExtensions
{
public static void TrimAll(this List<string> stringList)
{
for (int i = 0; i < stringList.Count; i++)
{
stringList[i] = stringList[i].Trim(); //warning: do not change this to lambda expression (.ForEach() uses a copy)
}
}
}
Use it like this:
像这样用它:
var productNumbers = new List<string>(){ "11111", " 22222 " }
productNumbers.TrimAll();
should result in: List(){ "11111", "22222" }
应该导致:List(){“11111”,“22222”}
I didn't use the split and re-join solution (chosen solution) because there can be a comma inside one of the string items. The regex version is not self explanatory. This is old-school but safer and can be easily understood...
我没有使用拆分和重新加入解决方案(选择的解决方案),因为其中一个字符串项可能有一个逗号。正则表达式版本不是自我解释。这是老派但更安全,可以很容易理解......