将字符追加到列表字符串 [英] Appending characters to a List string
本文介绍了将字符追加到列表字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在应用程序中有一个可选的自定义前缀和后缀,我想将其添加到字符串列表中的每个项目中.我尝试了以下所有方法,但均无效果.有人可以指出我正确的方向吗?
I have an optional custom prefix and suffix in my application, that I want to add to each of the items in my string List. I have tried all of the following and none are working. Can someone point me in the right direction please?
List<string> myList = new List<string>{ "dog", "cat", "pig", "bird" };
string prefix = "my ";
string suffix = " sucks!";
StringBuilder sb = new StringBuilder();
sb.Append(suffix);
sb.Insert(0, prefix);
MyList = sb.ToString(); //This gives me red squigglies under sb.ToString();
我也尝试过:
myList = myList.Join(x => prefix + x + suffix).ToList(); //Red squigglies
和:
sortBox1.Join(prefix + sortBox1 + suffix).ToList(); //Red squigglies
我在哪里错了?
推荐答案
目前尚不清楚为什么在这里使用StringBuilder
,或者为什么要进行联接.听起来像是您想要的:
It's not really clear why you're using a StringBuilder
at all here, or why you'd be trying to do a join. It sounds like you want:
var suckingList = myList.Select(x => "my " + x + " sucks")
.ToList();
这是使用LINQ对列表中的每个项目进行投影的绝对正常方法.
This is the absolutely normal way of performing a projection to each item in a list using LINQ.
这篇关于将字符追加到列表字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文