Splitting a long string into lines

This is a very simple exercise, probably why googling it didn’t get any hits as most people are interested in splitting text read in from a file. I wrote an audit script that would write
to the ARS scheduled task ‘lastrunmessage’ attribute so that I could quickly see if there were any errors in the last run.

I then wrote an audit script to audit the audit script, as I like to be sure my scripts are running error free.

I’d like to say I don’t have any bugs in my production scripts but we both know that’s not true! Plus things can change so a script that’s has run for years might suddenly stop. I had this exact issue with my scripts sending to an SMTP relay but that’s another post to come next.

Sometimes the script would generate multiple error messages so I stick them all together on the last run message but I discovered that the email alert generated was a little messy so I
wanted to split the looooooooooooooooooooooooooong lastrunmessage into a number of lines.

A quick function later and it was done. I didn’t even have to write a recursive function. The function has two parameters: stringLength, this is the length of the lines I want produce and string, which is the string I want to split up into lines.

I could have course added CRLF into the string and reconstituted the string but here all I wanted was an array of stings where the length didn’t exceed the stringLength parameter. I split the input sting using the ‘space’ as the delimiter.

$words = $string.split(" ")

Then I iterate through the words keeping any eye on the string length

ForEach ( $word in $words ) {
  if ( ( $newString.length + $word.length ) -gt $stringLength ) {

and rebuild the string as I go

  $newString += " $word "

until I hit the ‘stringLength’ limit when I add the current ‘newString’ to the array of ‘strings’ that the function will return.

  if ( ( $newString.length + $word.length ) -gt $stringLength ) {
   $strings += $newString
   $newString = $word
  }

the function will always return an array even if the input string is under the stringLength limit.

Here’s the function and an example command line to make use of the function.

Function Create-StringArray {
 param (
  $stringLength = 100,
  $string
 )
 $strings = @()
 if ( $string.Length -le $stringLength ) {
  $strings += $string 
  Return ,$strings # string is not big enough to other splitting so just return it as an array of 1 string
 }
 $words = $string.split(" ")
 $newString = ""
 ForEach ( $word in $words ) {
  if ( ( $newString.length + $word.length ) -gt $stringLength ) {
   $strings += $newString
   $newString = $word
  }
  elseif ( $newString -eq '' ) {
   $newString = $word 
  }
  else { 
   $newString += " $word "
  }
 }
 if ( $newString -ne '' ) {
  $strings += $newString
 }
 Return ,$strings 
}

$inputString = "this is a long string that is made up of words that would make it too long to display in a file without bad word wrapping happening"
ForEach ( $line in $(Create-StringArray -string $inputString) ) {
 $line 
}
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 )

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.