Why is Trim() Method not trimming?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Lorebass
    New Member
    • Sep 2013
    • 2

    #1

    Why is Trim() Method not trimming?

    ***Please pretend that any "-" is actually a white space.***

    I have a string DDInfo that has read a line from a text file and it says:

    " //SORTLIB DD DSN=SYS1.SORTLI B,DISP=SHR---------------------------0000380"

    I remove certain unnescesary information and come up with this:

    "DSN=SYS1.SORTL IB,DISP=SHR--------------------------"

    all i want to do is trim off the trailing whitespaces but the code i have will not do it.
    this is the entirety of what happens to the string:
    Code:
    DDInfo = sLine.Remove(73);
    DDInfo = DDInfo.Substring(16);
    DDInfo.Trim();
    The remove and first substring work perfectly but the problem is it still reads:

    "DSN=SYS1.SORTL IB,DISP=SHR--------------------------"

    Substring to remove the end doesn't work either.
    I feel like im missing something here. Im using Visual studio 2008.
    Last edited by Lorebass; Oct 21 '13, 02:09 PM. Reason: clarification
  • Joseph Martell
    Recognized Expert New Member
    • Jan 2010
    • 198

    #2
    An interesting tip from the MSDN page on the Trim() method:

    This method does not modify the value of the current instance. Instead, it returns a new string in which all leading and trailing white space characters found in the current instance are removed.

    Comment

    • Frinavale
      Recognized Expert Expert
      • Oct 2006
      • 9749

      #3
      Strings are immutable. This is why the Trim method returns a new string instead of effecting the string that it is being called.

      In other words, line 3 should be:
      Code:
      DDInfo = DDInfo.Trim();
      -Frinny

      Comment

      Working...