为什么我尝试修剪 List中的字符串会失败? 似乎不起作用?

发布于 2024-07-07 04:08:09 字数 412 浏览 8 评论 0原文

我在 LINQPad 中尝试了以下代码并得到了以下结果:

List<string> listFromSplit = new List<string>("a, b".Split(",".ToCharArray())).Dump();
listFromSplit.ForEach(delegate(string s) 
{ 
  s.Trim(); 
});
listFromSplit.Dump();

“a”和“b”

,所以字母 b 没有像我预期的那样删除空格......?

任何人都有任何想法

[注意:.Dump() 方法是 LINQPad 中的扩展方法,它以良好的智能格式方式打印出任何对象的内容]

I tried the following code in LINQPad and got the results given below:

List<string> listFromSplit = new List<string>("a, b".Split(",".ToCharArray())).Dump();
listFromSplit.ForEach(delegate(string s) 
{ 
  s.Trim(); 
});
listFromSplit.Dump();

"a" and " b"

so the letter b didn't get the white-space removed as I was expecting...?

Anyone have any ideas

[NOTE: the .Dump() method is an extension menthod in LINQPad that prints out the contents of any object in a nice intelligently formatted way]

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

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

发布评论

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

评论(8

写下不归期 2024-07-14 04:08:09

您只是创建一个修剪过的字符串,而不是为其分配任何内容。

var s = "  asd   ";
s.Trim();

不会更新,而..

var s = "   asd   ";
s = s.Trim();

将..

var listFromSplit = "a, b".Split(',').Select(s=>s.Trim());

我想,这就是我的处理方式。

you're just creating a trimmed string, not assigning anything to it.

var s = "  asd   ";
s.Trim();

won't update s, while..

var s = "   asd   ";
s = s.Trim();

will..

var listFromSplit = "a, b".Split(',').Select(s=>s.Trim());

would, i suppose, be how i'd go about it.

浅语花开 2024-07-14 04:08:09

String.Trim() 方法返回表示更新后的字符串的字符串。 它不会更新字符串对象本身,而是创建一个新的字符串对象。

您可以这样做:

s = s.Trim();

但是,您无法在枚举集合时更新集合,因此您需要在枚举现有列表时填充新列表,或者使用 String.Split 返回的字符串数组手动填充列表。

填充新列表:

List<string> temp = new List<string>("a, b".Split(",".ToCharArray()));
List<string> listFromSplit = new List<string>();

temp.ForEach(delegate(string s) 
{ 
    listFromSplit.Add(s.Trim()); 
});

listFromSplit.Dump();

手动填充:

string[] temp = "a, b".Split(",".ToCharArray());
List<string> listFromSplit = new List<string>();

foreach (string s in temp)
{
    listFromSplit.Add(s.Trim()); 
};

listFromSplit.Dump();

The String.Trim() method returns a string representing the updated string. It does not update the string object itself, but rather creates a new one.

You could do this:

s = s.Trim();

However you cannot update a collection while enumerating through it so you'd want to either fill a new List while enumerating over the existing one or populate the List manually using the string array returned by String.Split.

Filling a new list:

List<string> temp = new List<string>("a, b".Split(",".ToCharArray()));
List<string> listFromSplit = new List<string>();

temp.ForEach(delegate(string s) 
{ 
    listFromSplit.Add(s.Trim()); 
});

listFromSplit.Dump();

Populating Manually:

string[] temp = "a, b".Split(",".ToCharArray());
List<string> listFromSplit = new List<string>();

foreach (string s in temp)
{
    listFromSplit.Add(s.Trim()); 
};

listFromSplit.Dump();
短暂陪伴 2024-07-14 04:08:09

进一步回答 Adrian Kuhn 您可以执行以下操作:

var result = listFromSplit.Select(s => s.Trim());

Further to the answer posted by Adrian Kuhn you could do the following:

var result = listFromSplit.Select(s => s.Trim());
我乃一代侩神 2024-07-14 04:08:09

字符串实例是不可变的。 任何看似修改实例的东西都会创建一个新实例。

The string instances are immutable. Anything that seems to modify one, creates a new instance instead.

半边脸i 2024-07-14 04:08:09

您没有将修剪结果分配给任何东西。 这是一个典型的错误,我刚刚摆脱了使用 string.Replace 犯这个错误的习惯:)

You are not assigning the trimmed result to anything. This is a classic error, I've only just got out of the habit of making this mistake with string.Replace :)

ぶ宁プ宁ぶ 2024-07-14 04:08:09

我没有启动并运行 IDE,但这应该可以完成工作(除非我错了):

var result = from each in listFromSplit select each.Trim();

I have no IDE up and running, but this should get the job done (unless I am wrong):

var result = from each in listFromSplit select each.Trim();
儭儭莪哋寶赑 2024-07-14 04:08:09

按空格和逗号拆分并删除所有空条目。 一切都很好,修剪整齐。 不过,假设您的字符串不包含空格。

List<string> listFromSplit =
     new List<string>( "a , b ".Split( new char[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries ));

Split on both spaces and commas and remove any empty entries. All nice and trimmed. Assumes that your strings don't contain spaces, though.

List<string> listFromSplit =
     new List<string>( "a , b ".Split( new char[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries ));
枯叶蝶 2024-07-14 04:08:09

其他人提供的 linq 选项应该可以很好地工作。 作为另一种选择,这里是使用 for 循环的扩展方法:

    public static void TrimCollection(this IList<string> stringCollection) {

        for (int i = 0; i <= stringCollection.Count() - 1; i++)
            stringCollection[i] = stringCollection[i].Trim();

    }

The linq options others have provided should work well. As another option, here is an extension method using a for loop:

    public static void TrimCollection(this IList<string> stringCollection) {

        for (int i = 0; i <= stringCollection.Count() - 1; i++)
            stringCollection[i] = stringCollection[i].Trim();

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