String.Trim() does not work

  • Thread starter Thread starter C#Newbie
  • Start date Start date
C

C#Newbie

Ok I have this code in a function, but for discussion sake I will
revise it a little bit and make it shorter. The problem that I am
having is that trim does not work.

void main()
{
// message here is taken from a server and this is the string:
// start;
// a = 32, b = 33, c = 44;
// a = 33, b = 32, c = 55;
// end;

ParseMessage(Encoding.ASCII.GetString(message));

}

private void ParseMessage(string message)
{
string trimmedMsg = null;
string[] msgLine = null;
string[] msgItem = null;

char lineDelim = ';';
char itemDelim = ',';
char[] trimChar = {'='};

msgLine = message.Split(lineDelim);
foreach(string line in msgLine)
{
msgItem = line.Split(itemDelim);

foreach(string item in msgItem)
{
trimmedMsg = item.Trim(trimChar);
MessageBox.Show(trimmedMsg);
}

}

}


Please Help. Thank you
 
Your trimChar is not a leading or trailing character. I'm not sure what
you are wanting, but check out Replace, Substring, and/or IndexOf of the
String class.
 
C#Newbie said:
Ok I have this code in a function, but for discussion sake I will
revise it a little bit and make it shorter. The problem that I am
having is that trim does not work.

Trim certainly *does* work - but it may not be doing what you expect it
to do.

Could you give one specific string and what you expect Trim to return?
 
ahhh... oh yeah, string.trim only works if the trim characters are at
the beginning or at the end of string.


Thanks
 
C#Newbie said:
ahhh... oh yeah, string.trim only works if the trim characters are at
the beginning or at the end of string.

I think you actually just want to use Split again.
 
Back
Top