trim the string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?c2VlbWE=?=

    #1

    trim the string

    Hi,
    I have a string line = " DOL : 12/04/09 "

    I want to remove the white spaces and to get the string like
    line = "DOL : 12/04/09" ;

    i have th e following coding but it is not going to if statement.

    line.Trim();
    if (line.StartsWit h("DOL"))
    {
    start++;
    }
  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: trim the string

    seema,

    Calling methods that perform adjustments on the string actually return a
    new instance of a string with the adjustments applied. In this case, you
    have to do this:

    line = line.Trim();

    if (line.StartsWit h("DOL"))
    ....


    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m


    "seema" <seema@discussi ons.microsoft.c omwrote in message
    news:C873CE10-AE3A-4766-B539-836497AD3FDA@mi crosoft.com...
    Hi,
    I have a string line = " DOL : 12/04/09 "
    >
    I want to remove the white spaces and to get the string like
    line = "DOL : 12/04/09" ;
    >
    i have th e following coding but it is not going to if statement.
    >
    line.Trim();
    if (line.StartsWit h("DOL"))
    {
    start++;
    }

    Comment

    • =?Utf-8?B?S0g=?=

      #3
      RE: trim the string

      String is immutable; use:

      line = line.Trim();


      "seema" wrote:
      Hi,
      I have a string line = " DOL : 12/04/09 "
      >
      I want to remove the white spaces and to get the string like
      line = "DOL : 12/04/09" ;
      >
      i have th e following coding but it is not going to if statement.
      >
      line.Trim();
      if (line.StartsWit h("DOL"))
      {
      start++;
      }

      Comment

      • james

        #4
        Re: trim the string

        Trim returns a new string, so you need to do

        line = line.Trim();

        Strings in C# are immutable and cannot be changed.


        -James


        Comment

        • =?Utf-8?B?SmVyZW15?=

          #5
          RE: trim the string

          string line = " DOL : 12/04/09 ";
          string x = line.Trim();

          if(x.StartsWith ("DOL"))
          {
          //do something
          }

          "seema" wrote:
          Hi,
          I have a string line = " DOL : 12/04/09 "
          >
          I want to remove the white spaces and to get the string like
          line = "DOL : 12/04/09" ;
          >
          i have th e following coding but it is not going to if statement.
          >
          line.Trim();
          if (line.StartsWit h("DOL"))
          {
          start++;
          }

          Comment

          • =?Utf-8?B?SmVyZW15?=

            #6
            RE: trim the string

            without a new string variable or reassigning the value of line

            if(line.Trim(). StartsWith("DOL "))
            {
            //do something
            }

            "Jeremy" wrote:
            string line = " DOL : 12/04/09 ";
            string x = line.Trim();
            >
            if(x.StartsWith ("DOL"))
            {
            //do something
            }
            >
            "seema" wrote:
            >
            Hi,
            I have a string line = " DOL : 12/04/09 "

            I want to remove the white spaces and to get the string like
            line = "DOL : 12/04/09" ;

            i have th e following coding but it is not going to if statement.

            line.Trim();
            if (line.StartsWit h("DOL"))
            {
            start++;
            }

            Comment

            Working...