Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Substring word in C#

I want to get the substring XXX and ZZZ from my result text in c#
Text form: ("; XXX; ZZZ; WWW") but

Result.LastIndexOf(";")

Does not affect because I have ";" char for separate two word and I can't find index of first and second ";" char in my word.

like image 957
Hassan Fakhari Avatar asked Mar 09 '26 15:03

Hassan Fakhari


2 Answers

Split input string using string.Split() and remove spaces around using string.Trim():

var str = "; XXX; ZZZ; WWW";
var parts = str.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries)
               .Select(x => x.Trim());
foreach (var part in parts)
    Console.WriteLine(parts);

Result is exactly as asked in the question and without empty values:

XXX
ZZZ
WWW
like image 129
kyrylomyr Avatar answered Mar 11 '26 06:03

kyrylomyr


Here is the solution. Just split it by the special character.

string x = "; XXX; ZZZ; WWW";
string[] y = x.Split(';');

Now you have

y[0] = "";
y[1] = " XXX";
y[2] = " ZZZ";
y[3] = " WWW";
like image 43
MD.Tofael Ahmed Avatar answered Mar 11 '26 05:03

MD.Tofael Ahmed



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!