
Let's look at an example where we have the string " hello world" (that extra space at the front is what we want to get rid of). We can use Trim Whitespace to remove the leading space characters, as shown below.

One nice thing about (OpenG's) Trim Whitespace is that it is polymorphic and handles arrays of strings, as shown in this example:

Whitespace is also end of line characters, such as carriage returns (\r) and line feeds (\n). Yes, Trim Whitespace handles those, too.

If you want to give this VI a try, you can obtain it using VI Package Manager (VIPM). Simply select the OpenG String Tools (oglib_string) package from within VIPM. Once installed, this will add a new functions palette at OpenG>>OpenG String Tools. (See here for a quick guide on how to install OpenG on VIPM.)
I think that the best feature of this VI is that it is polymorphic. Otherwise, there is a LabVIEW VI ‘{vi.lib}\Utility\error.llb\Trim Whitespace.vi’ that is available (not sure what version it started but I believe it was LabVIEW 8.0).
There’s even a theory going around that it was the OpenG version that inspired the VI that ships with LabVIEW
This is truly one of my most frequently used Open G vi’s. Unfortunately I often forget it until a comparison fails in practice and I track it down to an untrimmed string. (The same goes for the toupper and tolower functions too. )
It is much rarer to need to compare two strings exactly than to compare to see if they are what a ‘regular human being’ would consider equal.
Well done
Keep up the good work.
I can end to the rumor that OpenG inspired the one in NI’s palettes. The rumor is false. I and another developer wrote Trim Whitespace.vi. Until this article, I didn’t even know that OpenG had an equivalent. We needed it, so we wrote it. You see, it just happens that Trim Whitespace was a VI that I couldn’t live without either.
John: I’ll often put this in my user interface code, inside the MyString Value Changed event, before I write the data to the data store.
Aristos: As they say, great minds think alike. Too bad NI doesn’t use OpenG, internally. It could probably save you all a lot of time
Dumb question time!
What kind of applications do Y’all develop that Trim White Space is so valuable?
I go three months or so between asking myslef “Where did I last see the “Trim White Spae” function?
Just curious,
Ben
Ben: Trim Whitespace“>Trim Whitespace can be useful for treating user-typed input or when trying to parse data from strings. Usually, it just adds a little insurance that the software will be more robust and work properly. Typically, you’ll want this after you convert delimited strings to arrays and you’ll want to make sure that you remove any leading and trailing whitespace around the data. For example, if you have the string:
” Monkey, Fish, Dog , Cat “
You might use String to 1D Array to convert this into the array:
[" Monkey", " Fish", " Dog ", " Cat "]
Passing this array into Trim Whitespace will return the array we really wanted:
["Monkey", "Fish", "Dog", "Cat"]
I hope that was a realistic enough example. It was the best I could do, having not had my first cup of coffee yet, this morning