[英]How to remove a specific string from list<strings>
I've got a list of strings called xyz the string has this structure iii//abcd, iii//efg. 我有一个名为xyz的字符串列表,该字符串具有此结构iii // abcd,iii // efg。 how can I loop through this list and remove only iii// ? 我如何遍历此列表并仅删除iii //? I have tried this but it remove everything. 我已经尝试过了,但是它删除了所有内容。 thanks 谢谢
string mystring = "iii//";
xyz.RemoveAll(x=> x.Split ('//')[0].ToString().Equals (mystring));
Removing all the strings who start with iii//
: 删除所有以iii//
开头的字符串:
xyz.RemoveAll(x => x.StartsWith(@"iii//"));
Removing the iii//
from all strings: 从所有字符串中删除iii//
:
var newList = xyz.Select(x => x.Replace(@"iii//", string.Empty)).ToList();
You can try this also which will remove the string from list if it starts with "iii/" other wise not. 您也可以尝试执行此操作,如果以“ iii /”开头的字符串从列表中删除,则以其他方式删除。
string mystring = "iii//";
xyz.RemoveAll(x=>x.StartsWith(mystring));
I believe OP wants something to remove iii//
from all strings: 我相信OP希望从所有字符串中删除iii//
:
string prefix = "iii///";
List<string> xyz = ...;
var result = xyz.Select(x => x.Substring(prefix.Length)).ToList();
Note: this of course assumes that each string really starts with prefix
. 注意:当然,这假设每个字符串实际上都以prefix
开头。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.