#PowerShell, #PowerWiseScripting, #ProjectWise, PWPS_DAB

A Little Fun with Strings

I had a user ask how they could build a file description using the values from four source strings. The caveat is that any of the strings could be null or empty. So, what to do?

The following are examples of the end result we are trying to achieve.

$FileDescription = "Desc1"
$FileDescription = "Desc1-Desc2-Desc3"
$FileDescription = "Desc1-Desc2-Desc3-Desc4"

The following are two ways I approached the problem.

Below are the four source strings we will be using as well as the target variable which will be populated with values from the source strings.

#Source Strings
$DescLine1 = 'Desc1'
$DescLine2 = 'Desc2'
$DescLine3 = 'Desc3'
$DescLine4 = 'Desc4'

# Target variable
[string] $FileDescription = [string]::Empty

Solution 1

In this solution, I put everything in one line. It’s a little confusing, but it works.

Each source string is interrogated to determine if it has been populated. If it has, the corresponding value will be added to the $FileDescription variable. If is has not, it simply does nothing. Notice, the last three source strings will be prepended with a dash when included.

# Looks at each string and if it is not empty, it is added to the $FileDescription variable.
$FileDescription = "$(if($DescLine1){"$DescLine1"})$(if($DescLine2){"-$DescLine2"})$(if($DescLine3){"-$DescLine3"})$(if($DescLine4){"-$DescLine4"})"

The following shows the result:

resultone

This works great when all source strings include a value. What if one does not?

# Set $DescLine2 to an empty string.
$DescLine2 = [string]::Empty

Now when it is run, the second source string is skipped. Looking good.

resulttwo

What happens if set source string one and three to null? We end up with a dash at the beginning of the line. Easy enough, we simply trim it off.

# Set $DescLine1 and $DescLine 3 to an empty string.
$DescLine1 = [string]::Empty
$DescLine3 = [string]::Empty

# Used to remove the preceding dash.
$FileDescription = $FileDescription.TrimStart('-')

Notice the preceding dash. In the first value returned.

resultthree

Just to make it more fun and confusing, we could wrap the entire right side of the equation with the trim method.

# Wrapped in the TrimStart() method.
$FileDescription = ("$(if($DescLine1){"$DescLine1"})$(if($DescLine2){"-$DescLine2"})$(if($DescLine3){"-$DescLine3"})$(if($DescLine4){"-$DescLine4"})").TrimStart('-')

Solution 2

Now, let’s do something that is a bit easier to read. Again, we will create an empty string to populate with the values from the four source strings.

# Target variable
[string] $FileDescription = [string]::Empty

Next, we will scrutinize each of the source strings to determine if they are null. Again, if one is null, it will be ignored. If not, we will include it in the target string. The difference with this approach is that we will only include the preceding dash, when the $FileDescription length is greater than zero when adding a source string value.

# If $DescLine1 contains a value, it will be added to the $FileDescription string.
if( -not ([string]::IsNullOrEmpty($DescLine1))) {
    $FileDescription = $DescLine1
}
# If $DescLine2 contains a value, it will be added to the $FileDescription string.
if( -not ([string]::IsNullOrEmpty($DescLine2))) {
    $FileDescription += "$(if($FileDescription.Length -gt 0){'-'})$DescLine2"
}
# If $DescLine3 contains a value, it will be added to the $FileDescription string.
if( -not ([string]::IsNullOrEmpty($DescLine3))) {
    $FileDescription += "$(if($FileDescription.Length -gt 0){'-'})$DescLine3"
}
# If $DescLine4 contains a value, it will be added to the $FileDescription string.
if( -not ([string]::IsNullOrEmpty($DescLine4))) {
    $FileDescription += "$(if($FileDescription.Length -gt 0){'-'})$DescLine4"
}

Experiment with it and have fun. Hopefully, you find this useful. Please let me know if you have any questions or comments.

If you like this post, please click the Like button at the bottom of the page.

 

 

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.