PowerShell CheatSheet – Working with strings
Like in previous “educationnal” post, I consider that theory is bullshit; only an example is valuable!
So here’s a few examples to illustrate what we can do with strings.
- Comparison
- Lower/Upper case
- Starts/Ends With
- Playing with letters
- Contains
- Where’s My
(Water)Letter? - Adding or remove a letter
- Replace a letter (or more)
- Join/Split
- (Durable) Split
- Bonus Tip
$string1="I am a string" $string2="I'm a string" $result=$string1.CompareTo($string2) if ($result -eq 0) { Write-Host "The 2 compared strings are equal" } elseif ($result -eq 1) { Write-Host "`"$string1`" is less than `"$string2`"" } elseif ($result -eq -1) { Write-Host "`"$string1`" is greater than `"$string2`"" } else {Write-Host "Couldn't compare `"$string1`" and `"$string2`""}
$string1="I am a string becoming... sorry, became upper" $string2="I AM A STRING BECOMING... SORRY, BECAME LOWER" $string1.ToUpper() $string2.ToLower()
$string1="I am a string. Do I start with a `"S`" like `"String`"?" $result=$string1.StartsWith('S') if (!$result) {Write-Host "Of course not! My content is checked, not my name..."} else {Write-Host "PowerShell has probably some issues with spelling today!"} $string2="I am a string. Do I end with a `"g`" like in the end of the word `"String`"?" $result=$string2.EndsWith('g') if (!$result) {Write-Host "Of course not! My content is checked, not my name..."} else {Write-Host "PowerShell has probably some issues with spelling today!"}
$string1="I am a string. Do I contain the letter `"s`" like `"string`"?" $result=$string1.Contains('s') if ($result) { Write-Host "Yes! But how many times?" $occurrences=[regex]::matches($string1,"s").count Write-Host "The letter `"s`" is contains $occurrences times" } else {"Nope!"} Write-Host "Ok, but how many letters (total) do I have?" $result=$string1.length Write-Host "$result letter(s)"
$string1="I am a string. Ok, we knew it... But can I know where's my FIRST `"s`"?" $firsts=$string1.IndexOf('s') $firsts+=1 #Yes, we have to add 1 because the first position begins at '0' Write-Host "Of course we can! Your FIRST `"s`" is the ${firsts}th letter in the string" Write-Host "And what about the LAST `"s`"?" $lasts=$string1.LastIndexOf('s') $lasts+=1 #Yes, we have to add 1 because the first position begins at '0' Write-Host "Come on! Your LAST `"s`" is the ${lasts}th letter in the string"
$string1="I am a funny string" $comment="and as I am a Police fan, now I would like to be a..." $positions=$string1.IndexOf("r") $leftPartofString=$string1.Substring(0, $positions) $rightPartofString=$string1.Substring($positions+1) $string2="$leftPartofString" + "$rightPartofString" $string2=$string2.Substring(4) Write-Host "$string1 $comment" Write-Host "$string2!" $string2=$string2.Substring(9) $string2=$string2.Insert(2, "R") Write-Host "OK, I'm still a simple $string2..."
$string1="string" Write-Host "I don't want to be a $string1 anymore... Please do something!" $string2=$string1.Replace('string', 'integer') Write-Host "Now we can say I'm an ${string2}. Even if it's not my real type..."
$string1="I am a string." $string2="I'm your wife!" $result=$string1 + " " + $string2 Write-Host "$result"
Write-Host "And we can get divorced!" $result.split(".")
$string="I want to be independant! Me too..." $splitedstring=$string.split("!") $string1=$splitedstring[0] $string2=$splitedstring[1] Write-Host "Content of separated `"`$string1`" is: $string1" Write-Host "Content of separated `"`$string2`" is: $string2"
Write-Host "We can imagine a string containing a number. If we don't need to calculate something with it..." Write-Host "We can also imagine this string is hum... my salary!" Write-Host "Of course, it's an example so I want to be rich!" Write-Host "So my salary will be contains in `"`$Salary`"." Write-Host "And its value will be 200K€/year. So let's affect `"200`" to `"`$Salary`"." $Salary="200" $BadResult="My Salary is $SalaryK€/year" Write-Host "$BadResult" Write-Host "Not good at all..." $BadResult="My Salary is $Salary K€/year" Write-Host "$BadResult" Write-Host "Better? Yes, because we now have the value, but what I need is to get rid of this space..." Write-Host "This one is fine: My Salary is ${Salary}K€/year"