String.Trim() does not work

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
 
O

O.B.

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.
 
J

Jon Skeet [C# MVP]

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?
 
C

C#Newbie

ahhh... oh yeah, string.trim only works if the trim characters are at
the beginning or at the end of string.


Thanks
 
B

Ben Voigt

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top