Categories

16068

Working with strings: bits and pieces

In the previous post I showed how to generate the parent OUs by splitting and rejoining a string.

I used the operators –split and –join

These were introduced in PowerShell v2.  In PowerShell v1 we had to use the split() method and stitch the thing back together ourselves using string concatenation.

lets start with a string

$string = "aaa#bbb#ccc#ddd#fff"

Not very imaginative but it works

We can split it up

PS (3) > $string -split "#"
aaa
bbb
ccc
ddd
fff

Notice we lose the character used for splitting. Just for fun we also have –isplit and –csplit available if we need them

The delimiter is the character( s ) we use as the dividing points.  need to be aware if splitting on a “.” 

Lets change all the “#” to “.”

$string2 = $string.Replace("#",".")

$string2 -split "." doesn’t quite give is what we want.  We need to use

$string2 -split "\."

Regular expressions are used in the background and “.” means any character so we use a “\” too escape it to a literal character

Alternatively use

$string2 -split ".",0,"simplematch"

where 0 indicates the number of substrings returned – 0 implies all

Try these to see the differences

$string2 -split ".",1,"simplematch"
$string2 -split ".",2,"simplematch"
$string2 -split ".",3,"simplematch"
$string2 -split ".",4,"simplematch"
$string2 -split ".",5,"simplematch"
$string2 -split ".",6,"simplematch"

 

Other  options available are

 

"SimpleMatch [,IgnoreCase]"

 

"[RegexMatch] [,IgnoreCase] [,CultureInvariant]

[,IgnorePatternWhitespace] [,ExplicitCapture]

[,Singleline | ,Multiline]"

 

see get-help about_split

Working with strings: I’m a substitute

PowerShell string substitution is a very neat trick

PS (1) > $string = "World"
PS (2) > "Hello $string"
Hello World

 

We can substitute the value of the variable into the string. The catch – it only works with double quoted strings i.e. we use “”

If we use single quotes ‘’

PS (3) > 'Hello $string'
Hello $string

 

we don’t get the substitution. One point to not though is that in this case

PS (4) > "Hello '$string'"
Hello 'World'

 

we do get substitution because the single strings are classed as part of the overall string.

Simple rule – when you need to substitute values into a string use double quotes

Working with strings: likes and dislikes

Lets start with a string

$str = "abcdefghi"

We often only have part of a string to work with so we end up using –like


$str -like "abc*"
$str -like "*abc*"
$str -like "*def*"
$str -like "*ghi"

 

these all return True

 

As you might expect

$str -like "*ihg"

returns false

A * is the standard wildcard for any number of characters

The negative of –like  should be –dislike but the PowerShell team went with –notlike instead so

$str -notlike "*ihg"

returns true

 

We can also match on single wild cards

$str -like "?bcdefghi"
$str -like "abcd?fghi"
$str -like "abcd??ghi"
$str -like "abcdefgh?"

 

All return true

We also have the explicit case versions – which of these returns false?

$str -like "ABCD*"
$str -clike "ABCD*"
$str -ilike "ABCD*"

 

yep its    $str -clike "ABCD*"

And just to round it out we can also do this

$str -notlike "ABCD*"
$str -cnotlike "ABCD*"
$str -inotlike "ABCD*"

 

which return false, true, false respectively

 

This is simple string matching using standard wildcards – if you want something more sophisticated its time to take a deep breath and hold it until next time when we look at using regular expressions

Working with Strings: Simple comparisons

Lets start with simple string matching as its a task that frequently comes along

"aaa" -eq "aaa"

"aaa" -ceq "aaa"

 

both return true.  What’s –ceq you ask?

PowerShell by design is case insensitive so for most operations it treats “aaa” and “AAA” identically.

using –eq tests in  a case insensitive manner and –ceq tests case sensitively so

"aaa" -ceq "AAA"    returns false but

"aaa" -eq "AAA"  returns true

 

Just to add to the complication we also have –ieq which tests in a case insensitive manner so

"aaa" -ieq "AAA" returns true as does

"aaa" -ieq "aaa"

 

Most of the time –eq will meet all your needs with –ceq as backup for case sensitive tests.  I rarely use –ieq

 

In other languages where the comparisons were case sensitive we would need to do one of these

"aaa".ToUpper() -ceq "AAA"

"aaa" -ceq "AAA".ToLower()

 

to avoid case issues but PowerShell just works as

"aaa" -eq "AAA"

 

When comparing strings use –eq if case doesn’t matter and –ceq if it does