I couldn’t live without “Trim Whitespace”

Posted on Saturday 28 June 2008

This is another article in a series showing some of my favorite OpenG VIs -- "The OpenG VIs that I couldn't live without". In this article, I'm going to show a very useful VI, Trim Whitespace, that is used for removing space, end of line, and other whitespace from at the beginning and/or end of a string.

Trim Whitespace

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.)


7 Comments for 'I couldn’t live without “Trim Whitespace”'

  1.  
    Omar Mussa
    June 28, 2008 | 4:49 pm
     

    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).

  2.  
    June 28, 2008 | 4:54 pm
     

    There’s even a theory going around that it was the OpenG version that inspired the VI that ships with LabVIEW ;)

  3.  
    john brohan
    June 29, 2008 | 4:38 am
     

    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.

  4.  
    Aristos Queue
    June 29, 2008 | 8:55 am
     

    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. :-)

  5.  
    June 29, 2008 | 12:26 pm
     

    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 ;)

  6.  
    Ben
    June 30, 2008 | 5:30 am
     

    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

  7.  
    June 30, 2008 | 6:26 am
     

    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 :)

Leave a comment

(required)

(required)


Information for comment users
Line and paragraph breaks are implemented automatically. Your e-mail address is never displayed. Please consider what you're posting.

Use the buttons below to customise your comment.


RSS feed for comments on this post |

 

Bad Behavior has blocked 1123 access attempts in the last 7 days.