473,563 Members | 2,805 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2833
[Removed comp.lang.c - why did you try to follow up there when this has
nothing to do with C?]

Kevin Smith <no@spam.comwro te:
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.co m>
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.comwri tes:
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.publi c.dotnet.langua ges.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_Keit h) 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.comwro te:
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.publi c.dotnet.langua ges.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.comwri tes:
>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.publi c.dotnet.langua ges.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.comwro te:
I want to be able to read answers at work which doesnt take
microsoft.publi c.dotnet.langua ges.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.co m>
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.comwro te:
I want to be able to read answers at work which doesnt take
microsoft.publi c.dotnet.langua ges.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.publi c.dotnet.langua ges.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.cause way.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.comwri tes:
>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.publi c.dotnet.langua ges.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.s tr[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
11636
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");
17
4646
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 analysis and I'm led to believe (but can't yet quantitatively establish as fact) that the two basic culprits are a lot of calls to: 1.) if(...
11
5335
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
13
2631
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');
22
9719
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...
26
3786
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() == "") {
1
6048
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...
121
5027
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
3243
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.
0
7665
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7583
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8106
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7642
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6255
agi2029
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...
1
5484
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5213
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3643
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...
1
2082
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 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.