Re: string.Trim() behavior

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Peter Duniho

    #1

    Re: string.Trim() behavior

    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
  • CBFalconer

    #2
    Re: string.Trim() behavior

    Peter Duniho wrote:
    >
    .... snip ...
    >
    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.
    Nonsense. Strings, in general, are modifiable. Some are not, for
    example in:

    char *s = "I am a string";

    where the pointer s can be modified, but not the string "I am ...".

    --
    [mail]: Chuck F (cbfalconer at maineline dot net)
    [page]: <http://cbfalconer.home .att.net>
    Try the download section.

    Comment

    • Keith Thompson

      #3
      Re: string.Trim() behavior

      CBFalconer <cbfalconer@yah oo.comwrites:
      Peter Duniho wrote:
      >>
      ... snip ...
      >>
      >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.
      >
      Nonsense. Strings, in general, are modifiable. Some are not, for
      example in:
      >
      char *s = "I am a string";
      >
      where the pointer s can be modified, but not the string "I am ...".
      It's not nonsense, it's merely off-topic. The OP's question was about
      C#, not C. Peter Duniho understandably didn't notice the
      inappropriate "Followup-To: comp.lang.c" header.

      --
      Keith Thompson (The_Other_Keit h) kst-u@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"

      Comment

      Working...