Showing posts with label String Manipulation in PowerShell. Show all posts
Showing posts with label String Manipulation in PowerShell. Show all posts

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

Tuesday, February 7, 2012

Part-5: Text Manipulation in PowerShell using .Contains() and .Equals() 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.

 

Hi,

today we are using .Contains() and .Equals() method on our text. The output of these methods is Boolean which mean either “True” or “False”

Lets start.

first set our $text variables , today we are going to use  3 variables "$text,$text2 and $text3

$text = "The quick brown fox runs over the lazy dog"

$text2 = "Tommy is not a lazy dog"

$text3 = "The quick brown fox runs over the lazy dog"

07-02-2012 13-43-05 

.Contains()

.Contains() method  check for the  word which we provide in brackets in to the original string. Let me show you.

$text.Contains("brown")

I am checking that if our $text variable contains the word “brown” in it  , and yes we have “brown” word in our $text variable so the out is “True”

07-02-2012 13-46-33

lets check the another example.

$text.Contains("red")

Now we are checking that if we have a “red” word in our $text variable. we know that our $text is set to  “"The quick brown fox runs over the lazy dog"” and there is no “red” in it so that’s why output is false.

07-02-2012 13-50-37

.Equals()

Like .Contains(), .Equals() also give output in Boolean. .Equal() method checks that both Strings are equal or not. the syntax is .Equal and provide the string to match in brackets().

Let see the examples.

here are our $text variables

$text = "The quick brown fox runs over the lazy dog"

$text2 = "Tommy is not a lazy dog"

$text3 = "The quick brown fox runs over the lazy dog"

---------

$text.Equals("$text3")

First i am checking $text and $text3 variable. as you seen $text and $text3 are identical so the output of the .Equal() is true.

07-02-2012 14-08-09

Lets check $text variable with $text2 variable.

$text.Equals("$text2")

We know that $text variable and $text2 variables are not same and when we have used the .Equal method to compare both variables the output is “False”

 

07-02-2012 14-12-13

Videos

Use Contains() method

 

Use Equal Method

Thanks for viewing.

Aman Dhally

messenger_freak_adp1 (30)

Monday, February 6, 2012

Part-4: Text Manipulation in PowerShell using .Replace(), and .Remove() 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
Hi,
Today we are using .replace and .remove methods to play with the string. Lets start. Today i am using some fake email id as text.
$text = Aman.Dhally@xyz.com
06-02-2012 12-48-52
Replace                       
Using replace we can replace the text,symbols of our strings.  The syntax is simple Replace(“Old Character”,”new character”)
First lets replace “Aman” with some other name , for example with “SAM”,"
$text.Replace("Aman","Sam")
You can see in below screenshot that  text “aman.dhally@xyz.com” is replaced with “Sam.Dhally@xyz.com

06-02-2012 12-52-28 
Secondly lets replace “.” with a blank spaces
$text.Replace("."," ")
In the screenshot you can se that all “.” are replaced with the “ “ a black space.
06-02-2012 12-55-42
lets assume that we have a text file which contain list of all users email id in our “XYZ” company and we want to remove “@xyz.com” and keep the text before “@xyz.com” .
$text.Replace("@xyz.com"," ")
and here is the our output :) ,showing only the text before “@xyz.com
06-02-2012 13-01-11
Remove                                
Before doing anything else first lets check the length of our text using Length property. The syntax for remove method is to define to keep the number of characters and remove everything else. Not getting my point.. let ,me show to you.
Our text have 19 characters in it.
$text.Length

06-02-2012 13-21-38 
$text.Remove(4)
You can see that i mentioned 4 characters to keep and remove everything else. and the output is “Aman” which are exactly 4 characters.
06-02-2012 13-22-55
$text.Remove(11)
Now i am keeping 11 characters of the string and removing everything else. and you can see that the output is  “Aman.Dhally” which are exactly 11 characters .
06-02-2012 13-25-15
there is another example of Remove() method. 
$text.Remove(4,7)
In this example. first you can see our original text “Aman.Dhally@xyz.com”, in second we are keeping the first 4 characters of the text and removing everything else, so the output is “Aman”, in third we are keeping first 4 characters of the text "which is “AMAN”, and then removing next 7 characters of the text which is “.Dhally”  and then the output is “Aman@xyz.com

06-02-2012 13-34-04

Video:
















Thanks for reading
Aman Dhally

Wednesday, January 25, 2012

Part-3: Text Manipulation in PowerShell using .StartsWith() and EndsWith() methods

 

Part-1: http://newdelhipowershellusergroup.blogspot.com/2012/01/part-1-text-manipulation-in-powershell.html | using Trim(),TrimEnd(),TrimStart()

Part-2: http://newdelhipowershellusergroup.blogspot.com/2012/01/part-2-text-manipulation-in-powershell.html | ToUpper(),ToLower()

Hi,

Today we are going to use .StartsWith() and .EndsWith() methods to play with our text.

we are using the same text which we used in previous posts and saving the text in a variable $text

$text = "The Quick brown fox jumps over the lazy dog"

24-01-2012 15-14-58 

Lets start.

.StartsWith()

This test that check that the string start with the character which we specified in .StartWith() and it give result is Boolean, the result is either “True” or “false”

lets check if our text start with “The Quick”

$text.StartsWith("The Quick")

the result is “True” , which means yes our text is start with “The Quick”

24-01-2012 15-21-40

NOTE: By default the the text which we specified in  StartsWith() and EndsWith() are case sensitive. Not getting my point ? let me show you.

this is our default text. “T” in The and “Q” in Quick is uppercase.

24-01-2012 15-14-58 

this time i type all lower case character in .StartsWith() method and lets see the result

$text.StartsWith("the quick")

and this time result is false. You need take care of it if you are going to use these methods in your scripts.

24-01-2012 15-30-58

lets compare.. I hope everything is clear now.

24-01-2012 15-32-45

How to fix it ???

to make these methods make case-insensitive add ,1 after the text. This ,1 is in igonecase  switch which is Boolean, so 1 means is case-insensitive is ON

$text.StartsWith("the quick",1)

Bingo !!! now our condition is true.. :)

24-01-2012 15-43-47 

Lets See the comparison chart. The result are in the front of you…

24-01-2012 15-45-23 

.EndsWith()

lets check if out text ends with “lazy dog”

$text.EndsWith("lazy dog")

The result is “True” which means our text ends with “lazy dog”

24-01-2012 15-48-59

as i mentioned before that the text in these Methods take as case sensitive. in our current string “lazy dog” is in lower case let try to search with “Lazy Dog”

$text.EndsWith("Lazy Dog")

as expected the result is “False”

24-01-2012 15-57-30

lets make it case-insensitive by ON the IgnoreCase switch

$text.EndsWith("Lazy Dog",1)

Bingoo !!! its true now ….

24-01-2012 16-09-31

want to see the comparison? I bet you want…

In the screenshot you can see the original text and the methods with ignorecase and without ignorecase switch

24-01-2012 16-11-05

Thanks

Aman Dhally

Aman Dhally

Tuesday, January 24, 2012

Part-2: Text Manipulation in PowerShell using .ToLower and .ToUpper method

 

Part-1: http://newdelhipowershellusergroup.blogspot.com/2012/01/part-1-text-manipulation-in-powershell.html

Hi,

In part-1 of text manipulation we saw how to use .Trim(),.TrimStart() and TrimEnd() method. Today we will see how to use .ToLower and ToUpper() Method.

Lets start

we are using the same text which we used in part-1 and saving the text in a variable $text

$text = "The Quick brown fox jumps over the lazy dog"

24-01-2012 12-42-07

.ToLower()

When we use ToLower() method it convert the whole string in lowercase.

Check first what $text contains.  and it contains the text which we provide to variable $text

24-01-2012 12-44-23

When we run the $text.ToLower() it converted the whole string in to lower case.

24-01-2012 12-46-13 

Let’s compare it with original one. You can see it converted the whole string to lower case {actually “T” in “The” and “Q” in “Quick” is uppercase in original text}

24-01-2012 12-49-00

ToUpper()

The ToUpper() method convert the whole string in to uppercase.

$text.ToUpper()

You can see that the whole string in converted to UPPER CASE :)

24-01-2012 12-55-24

let compare it with our original text.

Happy????? :)

24-01-2012 12-57-17

I hope it helps someone :)

Thanks

Aman Dhally

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