Monday, January 23, 2012

Part-1: Text Manipulation in PowerShell using Trim, TrimStart , TrimEnd methods


Hi,

Did you ever tried to use .Trim() method on a string and can’t get it working? if yes ? Then welcome to the novice group. and if no? man you are genius.

Kidding :)

i was looking for some example for it i found few but not with screenshots :) .. then i decide to write a article on it … 

So lets start.

I am using the text “The Quick brown fox jumps over the lazy dog” and put this text in to a variable $text
$text = "    The Quick brown fox jumps over the lazy dog.     "

You can see that i have a space in the the beginning of the text and the end of the text.



23-01-2012 16-26-58

Lets remove these spaces to remove these spaces we are going to use trim() method.
Trim()

The Trim() remove the black characters to the right and left 

$text.Trim()

You can see that white space is removed from beginning of “the quick brown”

23-01-2012 16-35-10

TrimStart()

The TrimStart() remove all the chracters from the start of the string.






If you want to remove any from the beginning of the text than we need to use TrimStart() method. Lets remove “The QUick” from the text.

$text.TrimStart("The Quick")

Now you can see in the output “The QUick” is removed from output.



23-01-2012 16-40-41

not lets trim the end of the string using TrimEnd() method

TrimEnd()

The TrimEnd() remove are the characters from the string.







$text.TrimEnd("the lazy dog.     ")

You can see that now “the lazy dog.” is removed from the output.


23-01-2012 17-37-57

I hope this helps someone…


Thanks
Aman Dhally

13 comments:

  1. For me it was a gret help. Thank you very much

    Norbert

    ReplyDelete
  2. Hi,

    You are welcome Nobert, i am glad that you find it helpful.

    Thanks
    Aman

    ReplyDelete
  3. Hey Aman,

    thank you for your description. You inspired me to write an article about Trim() many other functions in PowerShell, e.g. Substring(), Contains(), ToLower(), ToUpper(), StartsWith(), EndsWith().

    My article is in German. Here is the link:

    http://blog.stefanrehwald.de/2013/03/03/powershell-03-2-strings-bearbeiten-und-untersuchen-mit-funktionen-wie-trim-substring-contains-tolower-toupper-startswith-endswith/

    Thank you

    rewe

    ReplyDelete
    Replies
    1. Hi Deer,

      You are welcome and thanks for writing the same for your native language, I hope lots of users get benifitts from it.

      Keep it up the good and noble work.

      regards
      Aman Dhally

      Delete
  4. Thank you for writing this! It's going to help a lot in my project deployment.

    ReplyDelete
  5. Hey Aman,

    The TrimEnd and TrimStart examples are misleading. Consider the following statement:

    "The Quick brown fox jumps over the lazy dog".TrimEnd("dgo")

    This will trim "dog" from the end of the string. TrimStart and TrimEnd remove any consecutive concurrence of characters from the start or end from the string that are present in their parameter. This behavior can cause unexpected results in the following example:

    "The Quick brown fox jumps over the good dog".TrimEnd(" dog")

    You will find that " good dog" is trimmed.

    ReplyDelete
    Replies
    1. Hi Jan,

      You are right. Thanks for correcting me, I will update the post soon :)

      Thanks a ton.

      Regards
      Aman

      Delete
  6. Thanks for this. I have found a number of interesting articles now on this topic, but they are all about editing a predetermined string.

    I have been trying to figure out how to edit filenames using trim. We have a number of files with starting spaces that I'd like to get rid of.
    Any pointers, please? :-D
    I can't even figure what to google for :-/

    ReplyDelete
  7. Dear Aman, Thans for the great job explaining TrimStart. $text1.TrimStart("X-SKYPE-PSTNNUMBER:") >>zz1.txt worked like a charm:-)

    I have Skype VCF file and trying to get only name and phone number. For some reason I get error when using $text1.Substring(19,12) or $text1.Substring(19,$text1.length-19) - any tips would greatly appresicated. DD

    ReplyDelete
  8. I'm turning around on this and nothing found as yet for v5. Most of this .something on a string seems to have disappeared and no trace for an equivalent other than Select-Strung...
    Ideas anyone?

    ReplyDelete
    Replies
    1. After failing to find official info, I have found that the variable type generated by Select-String would return an object type 'Matchinfo' (?). I have explicitly declared the variable holding the result as [string]
      Use the $foo.GetType build-in function
      I have found all the built-in functions for a string variable
      Hope this helps someone
      ps5 powershell version 5 string manipulation

      Delete
  9. Hi,I am stuck at one point.The below trim command works at times and sometimes not!
    It is used to format the output csv file into two different columns and remove the semicolon
    (gc $FILENAME) | ? {$_.trimstart() -ne "" } | set-content $FILENAME

    Current output is as below which should remove the semicolons and format the output separately in two columns.Unfortunately its not working now!!!! :(((
    INSTANCE_NAME;STATUS
    "d0039r00 ;OPEN"
    "s0079r00 ;OPEN"
    "dbsla1 ;OPEN"
    "dbslb1 ;OPEN"
    "sbsla1 ;OPEN"
    "sbslb1 ;OPEN"
    "sbslc1 ;OPEN"
    "sbsld1 ;OPEN"
    "sbsle1 ;OPEN"

    ReplyDelete

Note: Only a member of this blog may post a comment.