Home > Software engineering >  How to use Trimstart(), TrimEnd() properly?
How to use Trimstart(), TrimEnd() properly?

Time:06-22

I am trying to parse something.

string str = @"UdPbC\0\s:SI0002,d:IC0002,n:234*66$SIACK,"   random.Next(4)   "*C4<CR><LF>\r\n";
str = str.TrimStart('$');
Debug.WriteLine(str);

It still gives me same sentence. I want to trim before '$'. How can i use trimStart properly?

CodePudding user response:

TrimStart() will only remove whitespaces, if you want to only select a specific section within a string, use SubString()

str = str.SubString(IndexOf('$'));

CodePudding user response:

You might even want to consider Split:

Enumerating them all:

string str = @"UdPbC\0\s:SI0002,d:IC0002,n:234*66$SIACK,"   random.Next(4)   "*C4<CR><LF>\r\n";
var data = str.Split('$');

foreach (var s in data)
    Debug.WriteLine(data[0]);

or just select the desired element directly:

string str = @"UdPbC\0\s:SI0002,d:IC0002,n:234*66$SIACK,"   random.Next(4)   "*C4<CR><LF>\r\n";
str = str.Split('$')[1];
  •  Tags:  
  • c#
  • Related