Powershell Interesting String Operations / Work Around

<########## Work around to remove string with Case In Sensitive Approach ##########>

$full_string = "My name is xyz !!!"
$rm_string = ''
if($full_string -match "XyZ") {
   $index = $full_string.indexof($matches[0])
   if($index -gt 0) {
      $rm_string = $full_string.remove($index)      
   }
}
write-host $rm_string
#Note: I like one liner solution if anybody has

Output:
Case-Sensitive Is Ignored !!!

<######## StartWith Function With Case In Sensitive ########>

$test = "Start word of string"
if($test.StartsWith("stArT","CurrentCultureIgnoreCase") ) {
    Write-Output "Case-Sensitive Is Ignored !!!"
}

Output:
Case-Sensitive Is Ignored !!!

<######## Difference Split String By Word & Character ########>

$test = "Start//word/of/string"

Write-Output "------------------"
Write-Output "Split By Character"
Write-Output "------------------"
$test.split('/') |% {
    Write-Output $_
}

Write-Output "------------------"
Write-Output "Split By Word"
Write-Output "------------------"
$test -split "//" |% {
    Write-Output $_
}

Output:
------------------
Split By Character
------------------
Start

word
of
string
------------------
Split By Word
------------------
Start
word/of/string

<######## Compare Multiple Words In String ########>

$test = ("1 2", "2 3", "1 5", "3 4")
$test |% {
    if($_ -match "3|4") {
        Write-Output $_
    }
}

Output:
2 3
3 4
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s