Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract text from string between commas

Tags:

c#

I want to extract only those words between two commas. So, if the string is Ab Java, DE, 78801 The answer must be DE I have tried this code but it is not working

string search = "Ab  Java, DE, 78801 ";
int index = search.IndexOf(",");
string result = search.Substring(search.IndexOf(",") ,index);
MessageBox.Show(result);
like image 720
Hisham shahid Avatar asked Dec 06 '25 04:12

Hisham shahid


1 Answers

If your string has always 2 commas, you can use String.Split for it like;

string search = "Ab  Java, DE, 78801 ";
Console.WriteLine(search.Split(',')[1]); // DE

Remember, this will you generate DE with an extra white space before it.

enter image description here

If you don't want that white space, you can use TrimStart() to remove it.

Console.WriteLine(search.Split(',')[1].TrimStart()); //DE
like image 62
Soner Gönül Avatar answered Dec 07 '25 17:12

Soner Gönül



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!