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

If you don't want that white space, you can use TrimStart() to remove it.
Console.WriteLine(search.Split(',')[1].TrimStart()); //DE
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With