Showing posts with label Trim. Show all posts
Showing posts with label Trim. Show all posts

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