string.Trim() Behavior

J

Jonathan Wood

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

Ryan Liu

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
 
J

jeremiah johnson

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

Tom Spink

Jonathan said:
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.

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

Jonathan Wood

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

Jonathan Wood

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

Chris Dunaway

Tom said:
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);
 
J

Jonathan Wood

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

Jon Skeet [C# MVP]

Jonathan Wood said:
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.
 
T

Tom Spink

Chris said:
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.
 
J

Jonathan Wood

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

Jonathan Wood

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

Tom Spink

Jonathan said:
Tom,


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

Jonathan Wood

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.
 

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