473,396 Members | 1,915 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,396 software developers and data experts.

string.Trim() behavior

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.

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.

Oct 8 '08 #1
8 2818
[Removed comp.lang.c - why did you try to follow up there when this has
nothing to do with C?]

Kevin Smith <no@spam.comwrote:
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?
None of the string methods *actually* modify the object they're called
on - strings are immutable. It's sloppy wording in the docs, basically
:(

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Oct 8 '08 #2
Kevin Smith <no@spam.comwrites:
According to the intellisense help, string.Trim() "Removes all occurances
or white space characters from the beginning and end of this instance."
[snip]

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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Oct 8 '08 #3
(reposting to the correct newsgroup...)

On Wed, 08 Oct 2008 11:26:01 -0700, Kevin Smith <no@spam.comwrote:
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');
No, it wouldn't. Nor would it modify the String instance that s refers
to. Strings are immutable. They are _never_ modified, by any method.
While the follow code DOES modify s.

s = s.Trim(\r');
Yes, the variable s is modified, so that it refers to a new instance of
the string with the whitespace removed. The original instance is not
modified.

Smack in the middle of the page documenting String.Trim(), there's a
clearly-marked "Note" answering exactly this question:
http://msdn.microsoft.com/en-us/library/t97s7bs3.aspx
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.
They do. None of them modify the actual instance.

I suppose you could argue that the method help in the IDE is wrong, but I
think it's simply so brief that it's ambiguous. The phrase "of this
instance" is simply telling you that you don't pass some other String
instance to the method, but rather it just operates on the instance used
to call the method.

Pete
Oct 8 '08 #4
Hi

I want to be able to read answers at work which doesnt take
microsoft.public.dotnet.languages.csharp, so I put followups to the more
common comp.lang.c group.

Best

KS
On Wed, 08 Oct 2008 12:25:54 -0700, Keith Thompson wrote:
Kevin Smith <no@spam.comwrites:
>According to the intellisense help, string.Trim() "Removes all occurances
or white space characters from the beginning and end of this instance."
[snip]

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.
Oct 8 '08 #5
Kevin Smith <no@spam.comwrote:
I want to be able to read answers at work which doesnt take
microsoft.public.dotnet.languages.csharp, so I put followups to the more
common comp.lang.c group.
That's not at all appropriate though - C and C# are completely
different languages.

Do you not have access to Google Groups from work?

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Oct 8 '08 #6
On Wed, 08 Oct 2008 13:01:21 -0700, Kevin Smith <no@spam.comwrote:
I want to be able to read answers at work which doesnt take
microsoft.public.dotnet.languages.csharp, so I put followups to the more
common comp.lang.c group.
Frankly, that is one of the most insane justifications for inappropriate
cross-posting and use of the Followup: field I have seen yet. You are
basically saying that you think it makes sense to clutter up the C
language newsgroup, for every single user of that newsgroup worldwide,
simply for your own convenience.

Please stop doing that. It's anti-social behavior, completely contrary to
the community standards on which Usenet was built.

It would be nice if your employer would not restrict your Internet access
so much that you can't even use the same news server there and where you
are now (presumably not at work, since the aioe.net server is a public,
free NNTP server, not your own work server). Barring that, it seems that
your employer is willing to make exceptions on their own NNTP server for
newsgroups that are business-related, so if you have no other way to
access the newsgroup, you'll have to ask them to include this newsgroup in
that list of exceptions.

And if they don't do that, and there's no alternative for reading this
newsgroup (e.g. Google Groups as Jon suggests), then you are stuck without
access to this newsgroup when you're at work.

Abusing a different unrelated newsgroup is going to get you kill-filed,
which is ultimately going to be far more counter-productive than not being
able to see replies while your at work would be.

Pete
Oct 8 '08 #7
Kevin Smith wrote, On 08/10/08 21:01:
Hi

I want to be able to read answers at work which doesnt take
microsoft.public.dotnet.languages.csharp, so I put followups to the more
common comp.lang.c group.
Why do you think it is appropriate to direct posts about c# to
comp.lang.c? C is a completely different language and C# IS NOT TOPICAL.

If your house is closer to a customer I'm visiting should I just barge
in to your house and stay? After all it is more convenient than staying
at home or paying for a hotel.

Please keep your C# somewhere it is topical. Also keep your top-posting
where it is acceptable, which is not here.

<snip>
>Anyone else replying to Kevin Smith's article, please *ignore* the
Followup-To header and post only to the csharp group. Thanks.
Please everyone keep the responses about csharp on the csharp group as
Kevin requested.
--
Flash Gordon
If spamming me sent it to sm**@spam.causeway.com
If emailing me use my reply-to address
See the comp.lang.c Wiki hosted by me at http://clc-wiki.net/
Oct 8 '08 #8
[Piggy-backing - followups set to comp.lang.c]

Keith Thompson said:
Kevin Smith <no@spam.comwrites:
>According to the intellisense help, string.Trim() "Removes all
occurances or white space characters from the beginning and end of this
instance."
[snip]

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.
Sorry, Keith, but my reply is C-relevant as well as C#-relevant.

(Incidentally, I'm piggybacking because my newsreader didn't see the
article in the normal feed, but I'm able to comment on the article because
my newsreader *could* find the article on an ID search - go figure,
because I can't.)

The question is:

"the follow code does not appear to modify s.

s.Trim('\r');

While the follow code DOES modify s.

s = s.Trim(\r');

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

And the answer is simple to deduce, but quite difficult to frame in a way
that is relevant in both cross-posted groups. Nevertheless, I intend to
try.

Operations take inputs and produce results. Very often, those operations do
not modify their inputs - and this is a Good Thing. If the subtraction
operator modified its input, we'd be furious: x = 6 - 2; would change 6
into 4, so printf("%d\n", 6) would produce 4 from now on!

Your code: s.Trim('\r'); is roughly analogous to x - 2. That is, if you
were to write this:

x - 2;

you would expect the code to do nothing. (You might even expect it to
result in a compilation error but, if so, you would be disappointed.)

But if you were to write this:

x = x - 2;

you would have every right to expect that x's value would be updated.

Now, C isn't C#, but it's not beyond the wit of mankind to imagine a C
variant in which strings are genuine first-class objects rather than mere
arrays of char. In such a language, one might reasonably implement a trim
function in a manner such as this:

string trim(string in)
{
string out = "";
size_t i = 0;
size_t j = in.length;
while(i < in.length && isspace(in.str[i]))
{
++i;
}
while(j 0 && isspace(in.str[j - 1]))
{
--j;
}
while(i < j)
{
out.append(in.str[i++]);
}
return out;
}

(Okay, so this isn't exactly legal C, but it wouldn't take much to make it
so.) The point is that this function accepts an input which it does not
modify - it creates a new object whose value it returns on completion. And
that's what your C# Trim function is doing, and that's why you have to say
s = s.Trim('\r').

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Oct 9 '08 #9

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...
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...
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...
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...
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: 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: 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
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
tracyyun
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...

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.