473,385 Members | 2,069 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,385 software developers and data experts.

string.Trim() Behavior

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 modify s.

s.Trim('\r');

While the follow code DOES modify s.

s = s.Trim(\r');

I understand that the help text quoted above is for the version of this
method that takes no arguments. But I would assume that variations of Trim
work the same fundamental way.

If this is modifying this instance, why do I only get the effect if I assign
the result?

Thanks.

--
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com
Jul 16 '06 #1
13 2612
Hi Jonathan,

string value is immutable.

A String is called immutable because its value cannot be modified once it
has been created. Methods that appear to modify a String actually return a
new String containing the modification. If it is necessary to modify the
actual contents of a string-like object, use the System.Text.StringBuilder
class.

(See:
ms-help://MS.NETFrameworkSDKv1.1/cpref/html/frlrfsystemstringclasstopic.htm)

so Trim method always return you a NEW string.

HTH,
Ryan
"Jonathan Wood" <jw***@softcircuits.comдÈëÓʼþ
news:Oz*************@TK2MSFTNGP04.phx.gbl...
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 modify s.

s.Trim('\r');

While the follow code DOES modify s.

s = s.Trim(\r');

I understand that the help text quoted above is for the version of this
method that takes no arguments. But I would assume that variations of Trim
work the same fundamental way.

If this is modifying this instance, why do I only get the effect if I
assign
the result?

Thanks.

--
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com


Jul 16 '06 #2
my GUESS is that it was written that way on purpose. Also, strings are
immutable. reassigning back to itself actually causes a new string
object to be created, thus it works.

but again, i'm guessing.

Jonathan Wood wrote:
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 modify s.

s.Trim('\r');

While the follow code DOES modify s.

s = s.Trim(\r');

I understand that the help text quoted above is for the version of this
method that takes no arguments. But I would assume that variations of Trim
work the same fundamental way.

If this is modifying this instance, why do I only get the effect if I assign
the result?

Thanks.

--
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com

--
jeremiah();
Jul 16 '06 #3
Jonathan Wood wrote:
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 modify s.

s.Trim('\r');

While the follow code DOES modify s.

s = s.Trim(\r');

I understand that the help text quoted above is for the version of this
method that takes no arguments. But I would assume that variations of Trim
work the same fundamental way.

If this is modifying this instance, why do I only get the effect if I
assign the result?

Thanks.

--
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com
Hi Jonathan,

While a string object is a reference object, it is classed as immutable,
meaning that you cannot modify it's value once it's created. The 'Trim'
method, and indeed all other string methods cannot alter the contents of
the string object, so they *must* return a new string object.

The line:
s = s.Trim('\r');

Does not modify the object, it reassigns 's' to the newly created string
object, that results from trimming '\r' from the current value of 's'.

--
Hope this helps,
Tom Spink

Google first, ask later.
Jul 16 '06 #4
Ryan,
string value is immutable.

A String is called immutable because its value cannot be modified once it
has been created. Methods that appear to modify a String actually return a
new String containing the modification. If it is necessary to modify the
actual contents of a string-like object, use the System.Text.StringBuilder
class.

(See:
ms-help://MS.NETFrameworkSDKv1.1/cpref/html/frlrfsystemstringclasstopic.htm)

so Trim method always return you a NEW string.
Thanks for the explanation. I can see it working that way but found the
intellisense help misleading. It seems to suggest it does exactly what you
are saying (and I can see) it does not do. But the explanation above helps
point me in the right direction.

Thanks.

--
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com
Jul 16 '06 #5
Tom,
While a string object is a reference object, it is classed as immutable,
meaning that you cannot modify it's value once it's created. The 'Trim'
method, and indeed all other string methods cannot alter the contents of
the string object, so they *must* return a new string object.

The line:
s = s.Trim('\r');

Does not modify the object, it reassigns 's' to the newly created string
object, that results from trimming '\r' from the current value of 's'.
Right, I understand that. I'm a long time programmer trying to make the jump
to .NET.

I guess I'd forgot that string types cannot be modified. I just found the
intellisense help completely misleading.

Thanks for getting me back up on the straight and narrow.

--
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com
Jul 16 '06 #6
Tom Spink wrote:
While a string object is a reference object, it is classed as immutable,
meaning that you cannot modify it's value once it's created. The 'Trim'
I think the OP understood this. He was questioning the docs where it
said:

According to the intellisense help, string.Trim() "Removes all
occurances
or white space characters from the beginning and end
**** of this instance.***" (<----- NOTE this phrase here)

The docs seem to indicate that the Trim function should operate on the
instance of the string when in reality it does not, it returns a new
string. Perhaps Trim should have been a static method so that it was
not ambiguous"

s = String.Trim(s);

Jul 17 '06 #7
Right. I'm not sure a static method would solve the issue. But I would
probably rewrite the help something like this:

"Returns a copy of this string with all occurances of white space characters
removed from the beginning and end."

--
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com

"Chris Dunaway" <du******@gmail.comwrote in message
news:11**********************@h48g2000cwc.googlegr oups.com...
Tom Spink wrote:
>While a string object is a reference object, it is classed as immutable,
meaning that you cannot modify it's value once it's created. The 'Trim'

I think the OP understood this. He was questioning the docs where it
said:

According to the intellisense help, string.Trim() "Removes all
occurances
or white space characters from the beginning and end
**** of this instance.***" (<----- NOTE this phrase here)

The docs seem to indicate that the Trim function should operate on the
instance of the string when in reality it does not, it returns a new
string. Perhaps Trim should have been a static method so that it was
not ambiguous"

s = String.Trim(s);

Jul 17 '06 #8
Jonathan Wood <jw***@softcircuits.comwrote:
Right. I'm not sure a static method would solve the issue. But I would
probably rewrite the help something like this:

"Returns a copy of this string with all occurances of white space characters
removed from the beginning and end."
To be fair, the "Return value" section of both overloads is more
accurate:

"Return Value
A new String equivalent to this instance after white space characters
are removed from the beginning and end."

and

"Return Value
The String that remains after all occurrences of the characters in
trimChars are removed from the beginning and end of this instance. If
trimChars is a null reference (Nothing in Visual Basic), white space
characters are removed instead."

The summary is far from ideal though, I agree.

--
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
Jul 17 '06 #9
Chris Dunaway wrote:
Tom Spink wrote:
>While a string object is a reference object, it is classed as immutable,
meaning that you cannot modify it's value once it's created. The 'Trim'

I think the OP understood this. He was questioning the docs where it
said:

According to the intellisense help, string.Trim() "Removes all
occurances
or white space characters from the beginning and end
**** of this instance.***" (<----- NOTE this phrase here)

The docs seem to indicate that the Trim function should operate on the
instance of the string when in reality it does not, it returns a new
string. Perhaps Trim should have been a static method so that it was
not ambiguous"

s = String.Trim(s);
I guess it depends on your point of view... because yes, string.Trim() does
remove all occurrances of white space characters from the beginning and end
of the instance. That's exactly what it does. It just returns a new
instance with the changes, not /adjust/ the current instance.

--
Hope this helps,
Tom Spink

Google first, ask later.
Jul 17 '06 #10
I was only referring to the Intellisense help. Since that is so convenient,
and real help takes several minutes to come up, I didn't even look at the
regular help. And since it's so convenient, I'm willing to assume there are
others who only see the Intellisense help as well.

--
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP************************@msnews.microsoft.c om...
Jonathan Wood <jw***@softcircuits.comwrote:
>Right. I'm not sure a static method would solve the issue. But I would
probably rewrite the help something like this:

"Returns a copy of this string with all occurances of white space
characters
removed from the beginning and end."

To be fair, the "Return value" section of both overloads is more
accurate:

"Return Value
A new String equivalent to this instance after white space characters
are removed from the beginning and end."

and

"Return Value
The String that remains after all occurrences of the characters in
trimChars are removed from the beginning and end of this instance. If
trimChars is a null reference (Nothing in Visual Basic), white space
characters are removed instead."

The summary is far from ideal though, I agree.

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

Jul 17 '06 #11
Tom,
I guess it depends on your point of view... because yes, string.Trim()
does
remove all occurrances of white space characters from the beginning and
end
of the instance. That's exactly what it does. It just returns a new
instance with the changes, not /adjust/ the current instance.
After further thought on this, I'd have to disagree. It does not remove
anything or modify in any way the string in the first instance. It creates a
copy that is a modification of the original. It simply does nothing to the
original string.

--
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com
Jul 17 '06 #12
Jonathan Wood wrote:
Tom,
>I guess it depends on your point of view... because yes, string.Trim()
does
remove all occurrances of white space characters from the beginning and
end
of the instance. That's exactly what it does. It just returns a new
instance with the changes, not /adjust/ the current instance.

After further thought on this, I'd have to disagree. It does not remove
anything or modify in any way the string in the first instance. It creates
a copy that is a modification of the original. It simply does nothing to
the original string.
Hi Jonathan,

Not exactly what I meant... I said that it doesn't remove or modify anything
in the current instance, you've got to think about it _really_ laterally...

You're exactly right it does nothing to the original string, *but* it is the
original string that is acted upon, i.e. the instance of that string.

It removes whitespace from that instance *of the string*, where *instance of
the string* means the actual value of the string.

I'm not trying to say that it modifies the string at all, I know it doesn't,
what I mean is that it the method acts on the value of the instance of the
original.

Like I said, you've got to think really laterally about it.

--
Hope this helps,
Tom Spink

Google first, ask later.
Jul 18 '06 #13
Tom,
It removes whitespace from that instance *of the string*, where *instance
of
the string* means the actual value of the string.
Well, instance is different from value. To me, instance implies the original
while value could be a copy.

At any rate, I think the Intellisense text is confusing and should be
rewored.

--
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com
Jul 18 '06 #14

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

Similar topics

4
by: knocker | last post by:
Hi I have a problem with JSP on websphere 5. When I try save information with swedish or danish ÅÄÖ characters, the string is cut where the first of these characters occurs. The JDK used is...
11
by: Darren Anderson | last post by:
I have a function that I've tried using in an if then statement and I've found that no matter how much reworking I do with the code, the expected result is incorrect. the code: If Not...
2
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...
1
by: Sankalp | last post by:
Hi, I am using VB 2005. My application has many data bound controls. The connection is stored in the app.config file. I want the application to start with a default connection string and while...
121
by: swengineer001 | last post by:
Just looking for a few eyes on this code other than my own. void TrimCString(char *str) { // Trim whitespace from beginning: size_t i = 0; size_t j; while(isspace(str)) {
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...
8
by: Keith Thompson | last post by:
Kevin Smith <no@spam.comwrites: You posted this to microsoft.public.dotnet.languages.csharp, where I presume it's topical. Why on Earth did you redirect followups to comp.lang.c? Anyone...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...

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.