How to Trim textarea content before feeding into an array

I’ve got a textarea that I’m using for multi-line input. I’m feeding the textarea contents into an array for later parsing. Sometimes, but not always, the data pasted into the textarea has leading spaces. I’m populating the array with the following code - what’s a good way to run a Trim while doing this, all together?

InputArray() = Split(TextArea1.Text, EndOfLine)
// Use better variable names for clarity in the future!
dim arsInputLines() as String = InputArray

for i as Integer = 0 to arsInputLines.Ubound
  arsInputLines(i) = trim(arsInputLines(i))
next

If you expect a lot of lines, then store the Ubound as a variable first so it doesn’t have to get counted again and again.

dim rxTrim as new RegEx
rxTrim.SearchPattern = "(?mi-Us)^[\\x20\\t]*(.*\\S)?[\\x20\\t]*$"
rxTrim.ReplacementPattern = "$1"
rxTrim.Options.ReplaceAllMatches = true

dim s as string = rxTrim.Replace( inputString )

Edit: Added the “?” to take care of lines that are only whitespace.

So Tim, I’m having a little trouble following what the first part your code is doing. The first line

dim arsInputLines() as String = InputArray

Are you loading the contents of my original array into another?

And then iterating through the elements of the array, and doing a trim individually?

It was a light-hearted stab at your variable naming. It’s not a necessary step.
But yes, my method iterates through each element to the array and trims them individually. This makes it so every line is trimmed.

If you only need to trim the beginning and end of the text in the textarea you can use

// what's wrong with this variable name? (I detest Hungarian notation)
InputArray() = Split(Trim(TextArea1.Text), EndOfLine)

Fyi, using Ubound in a loop will make that loop slower by a few milliseconds over millions of iterations. In other words, don’t worry about it. :slight_smile: