locked
Powershell: what is an equivalent of substring, ltrim, rtrim functions RRS feed

  • Question

  • Hello Forum,

    Are there any equivalents to substring, ltrim, rtrim functions in Powershell?


    gene
    Monday, November 14, 2011 6:07 AM

Answers

All replies

  • Hello Forum,

    Are there any equivalents to substring, ltrim, rtrim functions in Powershell?


    gene


    Here are the answers:

     

    $string = " BLAH "
    
    #LTrim
    $string.TrimStart()
    #RTrim
    $string.TrimEnd()
    #Substring
    $string.Substring(1,4)
    

    You can find more methods here

    http://msdn.microsoft.com/en-us/library/system.string_methods(v=VS.80).aspx


    Monday, November 14, 2011 6:17 AM
  • No error. You must have omitted the spaces before and after BLAH.

    (If you select, copy, and paste the code, no error occurs.)

    The effects are more obvious with these:

    "/$($string.TrimStart())/"
    "/$($string.TrimEnd())/"
    "/$($string.TrimStart().TrimEnd())/"

    You can also see the available methods with

    $string|gm


    • Edited by mrento Wednesday, June 10, 2015 6:43 PM add examples
    Wednesday, June 10, 2015 6:40 PM