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)

No comments:

Post a Comment