Amit Kumar
Amit Kumar

Reputation: 1059

how to remove string after specific character in list

I have a list as given below.

List<string> testing = new List<string>();
            testing.Add("1(1)(r)");
            testing.Add("2(4)");
            testing.Add("3(w)");
            testing.Add("33(4)");
            testing.Add("5(4)");
            testing.Add("6(6)");

Now my problem is that i want to remove full string on or after "(" my list should be something like as given below.

1
2
3
33
5
6

How can i solve this problem with lambda expression or is ther any other way to remove string after some specific character. Thanks in advance

Upvotes: 0

Views: 44

Answers (1)

Selman Gen&#231;
Selman Gen&#231;

Reputation: 101681

var result =  testing.Select(x => x.Split('(')[0]);

Another way:

var result =  testing.Select(x => x.Substring(0, x.IndexOf('(')));

Second approach will throw exception if one of your strings doesn't contain any (

Upvotes: 1

Related Questions