473,407 Members | 2,314 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

String.Trim() does not work

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

Oct 23 '06 #1
4 4661
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 wrote:
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
Oct 23 '06 #2
ahhh... oh yeah, string.trim only works if the trim characters are at
the beginning or at the end of string.
Thanks

Oct 23 '06 #3
C#Newbie <an********@gmail.comwrote:
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?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Oct 23 '06 #4

"C#Newbie" <an********@gmail.comwrote in message
news:11**********************@i42g2000cwa.googlegr oups.com...
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.
>

Thanks

Oct 23 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Chad Myers | last post by:
I've been perf testing an application of mine and I've noticed that there are a lot (and I mean A LOT -- megabytes and megabytes of 'em) System.String instances being created. I've done some...
4
by: Harold Hsu | last post by:
Hi all, We have a piece of code that reads data from SQL Server and data from Oracle. They are both strings. We are trying to compare the two strings (dr(column).ToString()) for equality. ...
4
by: jemptymethod | last post by:
http://htmatters.net/htm/1/2006/01/EIBTI-for-Javascript-explicit-is-better-than-implicit.cfm
2
by: claire | last post by:
I have a char that i convert to a string as follows m_tHeader.m_sshortname is defined as char; string result = new string(m_tHeader.m_sshortname); The problem is that any '\0' chars in the...
13
by: Jonathan Wood | last post by:
According to the intellisense help, string.Trim() "Removes all occurances or white space characters from the beginning and end of this instance." However, the follow code does not appear to...
22
by: Terry Olsen | last post by:
I have an app that makes decisions based on string content. I need to make sure that a string does not contain only spaces or newlines. I am using the syntax 'Trim(String)" and it works fine. I...
26
by: Neville Lang | last post by:
Hi all, I am having a memory blank at the moment. I have been writing in C# for a number of years and now need to do something in VB.NET, so forgive me such a primitive question. In C#, I...
2
by: bmerlover | last post by:
I want to parse out a string in Visual Studio .NET 2003 in C++. This is the format of the string: T1 = "Hello" It comes from a script file which I load into my gui app. It loads fine and I am...
8
by: Kevin Smith | last post by:
Hi, According to the intellisense help, string.Trim() "Removes all occurances or white space characters from the beginning and end of this instance." However, the follow code does not appear...
2
by: Peter Duniho | last post by:
On Wed, 08 Oct 2008 11:26:01 -0700, Kevin Smith <no@spam.comwrote: No, it wouldn't. Nor would it modify the String instance that s refers to. Strings are immutable. They are _never_...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.