locked
TrimStart method issue RRS feed

  • Question

  • Hello Everyone,

    Simple script :

    $result = "$/KORS/Release/Release31/KORS"
        $result = $result.TrimStart("$/KORS/")
    	Write-Output $Result

    Unfortunately it returns : "elease/Release31/KORS"  instead of "Release/Release31/KORS"

    When I change 'R' letter in KORS from 'R' to 'r' it works fine ie:

    $result = "$/KOrS/Release/Release31/KORS"
        $result = $result.TrimStart("$/KOrS/")
    	Write-Output $Result

    Release/Release31/KORS

    What do I wrong ?

    Thanks,



    • Edited by kelimak Wednesday, February 20, 2013 4:53 PM
    Wednesday, February 20, 2013 4:51 PM

Answers

  • That is the expected behavior. The TrimStart method trims all characters in the set from the beginning of the string; it does not remove a specified string.

    I think you want to do this instead:


    $result = $result -replace '^\$/KORS/', ''

    The -replace operator uses a regular expression. ^ means "beginning of line". Also the backslash (\) is needed to escape the $ because $ normally means "end of line". Also note that I enclosed the strings in single quotes to prevent PowerShell from attempting to expand variable names in the string.

    Bill

    • Proposed as answer by jrv Wednesday, February 20, 2013 5:33 PM
    • Marked as answer by kelimak Wednesday, February 20, 2013 6:57 PM
    Wednesday, February 20, 2013 5:02 PM

All replies

  • That is the expected behavior. The TrimStart method trims all characters in the set from the beginning of the string; it does not remove a specified string.

    I think you want to do this instead:


    $result = $result -replace '^\$/KORS/', ''

    The -replace operator uses a regular expression. ^ means "beginning of line". Also the backslash (\) is needed to escape the $ because $ normally means "end of line". Also note that I enclosed the strings in single quotes to prevent PowerShell from attempting to expand variable names in the string.

    Bill

    • Proposed as answer by jrv Wednesday, February 20, 2013 5:33 PM
    • Marked as answer by kelimak Wednesday, February 20, 2013 6:57 PM
    Wednesday, February 20, 2013 5:02 PM
  • Thank You Sir!
    Wednesday, February 20, 2013 6:57 PM
  • It seems to me .TrimStart() has a bug and trims certain words in a string more so than others.

    Working Example:

     'C:\Windows\system32\tasks\BCleanerSkipUAC'.TrimStart("C:\Windows\system32\tasks\")

    returns: BCleanerSkipUAC


    Real world problem:

    'C:\Windows\system32\tasks\CCleanerSkipUAC'.TrimStart("C:\Windows\system32\tasks\")

    returns: leanerSkipUAC

    Lowercase "c" is good.

    'C:\Windows\system32\tasks\ccleanerSkipUAC'.TrimStart("C:\Windows\system32\tasks\")

    returns: ccleanerSkipUAC

    .TrimStart() clearly has an issue with uppercase "C" following an backslash in the string.

    "\C" is an bug for trimstart()


    Thursday, November 20, 2014 10:52 AM
  • This question is already marked as answered. If you still need help, please start a new question.

    -- Bill Stewart [Bill_Stewart]

    Thursday, November 20, 2014 1:22 PM