简体   繁体   English

如何从列表中删除特定的字符串<strings>

[英]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.

相关问题 如何检查列表中是否存在特定字符串 <string> 如果不从列表中删除索引? - How can i check if the specific strings exist in a List<string> and if not to remove the indexs from the List? 如何从清单中删除 <string> 包含URL的URL中的特定字符串? - How can i remove from a List<string> that contain url's with specific strings in the url's? 如何从列表中的字符串中删除数字/数字 <string> ? - How can I remove numbers/digits from strings in a List<string>? 如何从另一个列表中删除列表中的字符串? - How to remove strings in list from another list? 如何从列表中删除其字符串包含子字符串的列表元素 - How to remove elements of a list where its string contains sub strings from another list 如何从列表对象中删除空字符串 - how to remove empty strings from list object 如何删除包含特定字符串的列表中的项目 - How to remove an item in a list that contains specific string C# 文本文件到字符串数组以及如何删除特定字符串? - C# text file to string array and how to remove specific strings? 如何检查一行是否包含字符串列表中的特定字符串? - How can I check if a line contains specific string from a list of strings? 如何使用从字符串到字符串列表的automapper - How to use automapper from string to List of Strings
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM