Frank Belu
Frank Belu

Reputation: 27

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));

Upvotes: 0

Views: 2653

Answers (3)

Konrad Kokosa
Konrad Kokosa

Reputation: 16878

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.

Upvotes: 0

Md.Rajibul Ahsan
Md.Rajibul Ahsan

Reputation: 853

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));

Upvotes: 0

Amir Popovich
Amir Popovich

Reputation: 29836

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();

Upvotes: 5

Related Questions