none
String.TrimStart Doesn't Work RRS feed

  • Question

  • Hi,

    I am trying to trim the leading tab from a string. It seems like it should be very simple, but it just doesn't work.

    I am using:

    csContentString.TrimStart('\t');

    Can somebody help?




    • Edited by Shawn Beachy Thursday, July 3, 2014 4:37 PM
    • Moved by Eric Fleck Thursday, July 3, 2014 8:58 PM .NET string class question
    Thursday, July 3, 2014 4:20 PM

Answers

  • Hi,

    Strings in .Net are immutable, meaning they can never be changed. None of the string manipulation functions actually change your string, but instead always return a new string. So, you need to capture the return value of these functions (otherwise you're just wasting CPU cycles!).

    E.g.

    string trimmedContent = csContentString.TrimStart('\t');

    One more thing to know, Strings in .Net are also reference types, not value types. This means you can change the string object that a string variable references;

    string myStr = "hello";
    myStr = "world";

    If you mistakenly think of strings as value types, you might be thinking that strings really are mutable, becuase this code looks like it changes a string, but in fact there are two string objects here, and we just changed the one that we referenced.

    So, finally, you can write your code like this:

    csContentString = csContentString.TrimStart('\t');

    Which creates a new trimmed string, and the sets csContentString to reference it (The original string will be garbage collected).

    (A long-winded answer indeed, but I've spent far too many hours debugging code written by people who refuse to be disabused of the notion that strings are mutable value types. They are immutable reference types)

    Regards,
    Nick.

    • Marked as answer by Shawn Beachy Monday, July 7, 2014 12:48 PM
    Friday, July 4, 2014 6:58 AM
  • See code below

               string csContentString = "\t";
                csContentString = csContentString.TrimStart('\t');


    jdweng

    • Marked as answer by Shawn Beachy Friday, August 1, 2014 5:56 PM
    Thursday, July 3, 2014 10:07 PM

All replies

  • See code below

               string csContentString = "\t";
                csContentString = csContentString.TrimStart('\t');


    jdweng

    • Marked as answer by Shawn Beachy Friday, August 1, 2014 5:56 PM
    Thursday, July 3, 2014 10:07 PM
  • Hi,

    Strings in .Net are immutable, meaning they can never be changed. None of the string manipulation functions actually change your string, but instead always return a new string. So, you need to capture the return value of these functions (otherwise you're just wasting CPU cycles!).

    E.g.

    string trimmedContent = csContentString.TrimStart('\t');

    One more thing to know, Strings in .Net are also reference types, not value types. This means you can change the string object that a string variable references;

    string myStr = "hello";
    myStr = "world";

    If you mistakenly think of strings as value types, you might be thinking that strings really are mutable, becuase this code looks like it changes a string, but in fact there are two string objects here, and we just changed the one that we referenced.

    So, finally, you can write your code like this:

    csContentString = csContentString.TrimStart('\t');

    Which creates a new trimmed string, and the sets csContentString to reference it (The original string will be garbage collected).

    (A long-winded answer indeed, but I've spent far too many hours debugging code written by people who refuse to be disabused of the notion that strings are mutable value types. They are immutable reference types)

    Regards,
    Nick.

    • Marked as answer by Shawn Beachy Monday, July 7, 2014 12:48 PM
    Friday, July 4, 2014 6:58 AM