简体   繁体   中英

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. how can I loop through this list and remove only 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// :

xyz.RemoveAll(x => x.StartsWith(@"iii//"));

Removing the iii// from all strings:

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.

      string mystring = "iii//";
      xyz.RemoveAll(x=>x.StartsWith(mystring));

I believe OP wants something to remove iii// from all strings:

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM