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 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
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();
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.
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
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
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);
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);
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
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.
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
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
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.
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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))
{
|
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...
|
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...
|
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$) {
}
...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
| |