Thomas’ Developer Blog

May 4, 2008

Delimiter List Trimming VBScript

Filed under: Asp.Net Framework 2.0, vb.net, vbscript — Tags: , , , , , , , — sanzon @ 11:03 pm

Yes, as you notice if you read my post… I’m doing a ton of string modifying, so you’ll be seeing a lot of really simple examples, but I figure it’ll be good to help those who might have these  issues in the future.

Alright, continuing with the theme of the last two post!  I’m going to show you another trim method!  This method will go ahead and trim a comma list, or any other type of delimiter list.

The method is easy, you take a string seperated by a delimiter such as:

Hobbies:
”  Coding ,Gaming, Anime,Bowling, Pool,Business ,Finance,  ”

Now you will notice that there is a problem with the above code.  All of those spaces before and after the comma and at the end of the list.  Plus there is a comma at the end of the list, this is just a mess!

Well what we need to do is trim the edges of whitespace, remove the commas at the end, and check for spaces before and after the commas to make sure everything is all neat and tiddy!  Best part of this is, the user never notices!  Which is how you want it.  Make it easy for them, let them mess up the string as much as the like, we just have to go back in and fix it up before we take the list split it up and insert it into a database.

Well here is the next fun function to do just that!

   Public Function DelimiterListTrim(ByVal mySrc As String,Optional ByVal myDelimiter As String = “,”) As String
    If mySrc = “” Then
     return nothing
     exit function
    End If

    Dim myStr As String = mySrc

    Dim myChar As String = myDelimiter & chrW(32)

    myStr = Replace(myStr,chrW(13),””)

    do while Regex.IsMatch(myStr,myDelimiter & ” “)
     myStr = Replace(myStr,myChar,myDelimiter)
    loop
    
    do while Regex.IsMatch(myStr,” ” & myDelimiter)
     myStr = Replace(myStr,StrReverse(myChar),myDelimiter)
    loop

    do while Regex.IsMatch(Left(myStr,1),myDelimiter)
     myStr = mid(myStr,2)
    loop

    do while Regex.IsMatch(Right(myStr,1),myDelimiter)
     myStr = mid(myStr,1,len(myStr)-1)
    loop

    return myStr
   End Function

The code is fairly easy, it’s just what we did before, all I did this time was combine them!

First we create our string we want to test, which is the delimiter and any spaces.  Notice that the delimiter is optional and the default is comma.  This is the most common practice.  Well first we check for occurances of “, ” and swap it out with “,” and we do a loop because they may have “,  ” so we need to go through several times.  Again we do the same in reverse for checking for ” ,” once we have done that we simply go through and loop through to clean the edges of any excess delimiters towards the begining or end.

Now that string will come out neat and tidy as:

” Coding,Gaming, Anime,Bowling,Pool,Business,Finance”

And you can go use the split method to go ahead and add it to a database!  Well hope this helps, happy coding!

Advertisements

1 Comment »

  1. Nice writing style. I will come back to read more posts from you.

    Susan Kishner

    Comment by Susan Kishner — May 4, 2008 @ 11:06 pm


RSS feed for comments on this post. TrackBack URI

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

Blog at WordPress.com.

%d bloggers like this: