Monday, February 13, 2012

Part-6: Text Manipulation in PowerShell using .ToCharArray(),.PadLeft(), and .PadRight() methods

 

Part-1: Text Manipulation in PowerShell using Trim, TrimStart , TrimEnd methods
Part-2: Text Manipulation in PowerShell using .ToLower and .ToUpper method
Part-3: Text Manipulation in PowerShell using .StartsWith() and EndsWith() methods
Part-4: Text Manipulation in PowerShell using .Replace(), and .Remove() methods.
Part-5: Text Manipulation in PowerShell using .Contains() and .Equals() methods

Hi,

Today we are going to see what .Chars() , PadLeft() and Padright() does. And I think after covering this method there are only few methods left to cover for the next text manipulation series articles.

We are to using $text variable to show the mentioned methods.

 

$text = "The quick Brown fox"

13-02-2012 11-32-49

ToCharArray()

 

The .Chars() method convert the whole string to in to the array.

$a = $text.ToCharArray()

 

As you can see in the below screenshot it spilt the whole string in to the single character. This is converted in to a array object.

13-02-2012 11-37-23

How to check this is an array?

Okay! Let’s assign the above command to a another variable

$arr = $text.ToCharArray()

13-02-2012 11-41-23 

And now try to access the first object of array,the output is "T" and when we try to access the second object in the array the output is "H"

$arr[0]

$arr[1]

 

13-02-2012 11-44-03 

 

 

PadLeft()

 

The PadLeft() method basically added the specified number of space on the left of the sting, we need to specify how many black space we want to add in enclosed with bracket.

 

$text.PadLeft(200)

You can clearly see the blank spaces in the front of our original text.

 

13-02-2012 11-49-39

.PadRight()

The .PadRight() does the same thing but I added the specified number of space to the right side of the string.

 

Thanks for reading :)

Aman Dhally

1 comment: