Summary: Microsoft Scripting Guy, Ed Wilson, talks about several approaches for concatenating strings by using Windows PowerShell.
Microsoft Scripting Guy, Ed Wilson, is here. This weekend, I was speaking about blogging and technical writing at the South Carolina Writers Workshop in Rock Hill, South Carolina. One of the cool things is a couple of people drove from Atlanta and from Raleigh, North Carolina to attend my sessions. It was great to see them.
If you hang out with The Scripting Wife on Facebook, you know that we also moved over the weekend. It is just across town, but still it is a major disruption. This morning I am sitting on the floor in our new apartment, with my Surface Pro 2 propped up on a cardboard box full of books, and I am listening to Chick Corea on my Zune. I have my Zune plugged in to an old pair of powered “computer speakers” to make an impromptu stereo. Here is a picture of my current workstation:
I can’t find my Bose noise-cancelling headphones, or I would just use those. The nice thing is that our new apartment has free WiFi in the common areas, and the signal is strong enough in our apartment, so I am all set.
The cool thing about my Surface Pro 2 is that it, in fact, has Windows PowerShell. So I can write my blog post for today without any issues.
Joining strings
When it comes to the simple task of joining strings, Windows PowerShell offers multiple ways to accomplish this task. I know it can get somewhat complicated, but when I stop to think about it, the simple task of joining strings can get really complicated. For example, the easiest way to join two strings is to concatenate them. But even that choice has multiple options.
Concatenating strings
Let's take a look at how to concatenate (join together) a couple of strings. To do this, I will assign the strings to two variables. This is shown here:
PS C:\> $s = "This is a string"
PS C:\> $t = "a really cool string"
In Windows PowerShell the concatenation operator is also the one that is used for adding stuff together. So it sort of makes sense. To concatenate the string, I just add them together. Here is the command and the result:
PS C:\> $s + $t
This is a stringa really cool string
PS C:\>
WooHoo! It worked!
But wait. One might say it “messed up.” Actually, I wanted it to show up like that. Honest. Really. OK, maybe not.
What happens when concatenating two strings like that, is it glues them together. If I want a separator, I need to specify it. Here is my revision:
I use a comma and a space for my separator, and the output looks great:
PS C:\> $s + ", " + $t
This is a string, a really cool string
PS C:\>
Expanding strings
An easier way to concatenate the strings is to use double quotation marks because they automatically expand the contents of the variable. So, instead of multiple concatenation operators, all I need to do is put everything in a pair of double quotation marks. When I do that, I add my comma and my space inside the string. This can make it a bit difficult to read, but it works fine. This is shown here:
PS C:\> "$s, $t"
This is a string, a really cool string
PS C:\>
Substitution
Because the expanding string can become difficult to read, I often like to use substitution values and the format specifier. To do this, I create a string and parameters for place holders. My separator goes inside the string. Then I use the –f format operator and specify the values for {0} and {1}. This is shown here:
PS C:\> "{0}, {1}" -f $s,$t
This is a string, a really cool string
PS C:\>
Use the Join operator
Windows PowerShell has a Join operator that will glue strings together. It is a little funky and it takes a bit of practice to use, but when you understand it, it works well. Here is the way I first tried to use the Join operator:
PS C:\> $s -join $t
This is a string
PS C:\>
Needless to say, I was not impressed with the output. So I had to look up the about_Join topic in my online Help.
I learn that if I want to join the strings together, I need to put –Join at the beginning of the command as shown here:
PS C:\> -join $s, $t
This is a string
a really cool string
PS C:\>
Well OK, that worked. Sort of. But it does not look like what I had in mind when I wanted to join the two strings. It looks more like an array of strings, which I would pretty much have anyway. How about take number two:
PS C:\> -join ($s, $t)
This is a stringa really cool string
PS C:\>
This is what I was really expecting. It looks like I need to group the strings together, and then –Join will concatenate them. Groovy. But now I am back where I started. I need to figure out how to add a delimiter.
That is pretty easy. The online Help tells me that by default, there is no delimiter. But if I want a delimiter, I need to put it as the second argument to the operator. So here I go for attempt number four:
PS C:\> -join ($s, $t), ", "
This is a stringa really cool string
,
PS C:\>
Dude, that did not work either. Bummer.
I look at the Help topic a bit closer, and I discover that If I want to join a string with a specific delimiter, I put the string on the left side, and I put the delimiter on the right side. Attempt number five:
PS C:\> $s, $t -join ", "
This is a string, a really cool string
PS C:\>
And it works! Note that this time, I did not have to group my strings together—I was able to supply the strings as a simple array of strings.
So, like I said, the –Join operator is a little funky, but it does not take too much playing around to get it working properly.
That is all there is to joining strings. String Week will continue tomorrow when I will talk about using .NET Framework methods to join strings.
I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson, Microsoft Scripting Guy