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 2636
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.Str ingBuilder
class.
(See:
ms-help://MS.NETFramework SDKv1.1/cpref/html/frlrfsystemstri ngclasstopic.ht m)
so Trim method always return you a NEW string.
HTH,
Ryan
"Jonathan Wood" <jw***@softcirc uits.comдÈëÓʼ þ
news:Oz******** *****@TK2MSFTNG P04.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.Str ingBuilder
class.
(See:
ms-help://MS.NETFramework SDKv1.1/cpref/html/frlrfsystemstri ngclasstopic.ht m)
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.goo glegroups.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***@softcirc uits.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.co m> 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. 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 1.3.1
I've tried:
String CUNM = request.getParameter("CUNM").trim();
CUNM = URLDecoder.decode(CUNM,"UTF-8");
|
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 (strIn.Substring(410, 10).Trim = "") Then
'Something processed
Else
'Something processed
|
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 modify s.
s.Trim('\r');
While the follow code DOES modify s.
s = s.Trim(\r');
|
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 thought I'd change it to the VB
..NET method "String.Trim" but that throws an object exception.
Which brings the question: is it compliant to use Trim(String), or is it
more within etiquette to use If Not String Is Nothing Then String.Trim?
|
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 test whether a string has a value or not by the following syntax:
if (thisString.Trim() == "")
{
| |
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 during the runtime, the user can click on a button and change the
connection string without exiting the application.
I would really appreciate any sort of help.
|
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 to modify s.
s.Trim('\r');
While the follow code DOES modify s.
|
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 else replying to Kevin Smith's article, please *ignore* the
Followup-To header and post only to the csharp group. Thanks.
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |