Skip navigation

Create a Custom Function to Manipulate Strings

Downloads
7463.zip

Creating user-defined functions that meet your specific string-manipulation needs isn't difficult. You just need to be familiar with the built-in string-manipulation functions and know how to use the Function statement to create user-defined functions. (For information about the Function statement, see my October column.) To get you started, I'll explain how I created two user-defined functions: SentenceCase and SetIndexAt.

The SentenceCase Function
I created the SentenceCase function to make a string sentence case (i.e., a string in which all the letters are lowercase except the first letter). Although the SentenceCase function isn't the only way to write a function that makes a string sentence case, this function is quick and easy to understand.

The syntax for the SentenceCase function is

SentenceCase(str)

where str is the string you want to make sentence case. So, for example, the code

str = "HELLO, World!"
MsgBox SentenceCase(str)

displays Hello, world!

Listing A contains the code for the SentenceCase function. To create this function, I first used the Function statement to declare and define my function and Dim statements to declare the variables temp and firstChar. I then used LCase to lowercase the entire string, setting the result (in this example, "hello, world!") to temp. Using the Left function, I isolated the first letter in that string and set the result ("h") to firstChar. Finally, I used the Replace and UCase functions to uppercase the letter that firstChar contains and replace firstChar ("h") with that uppercase letter ("H"). To avoid problems, I used the Replace function's count argument to make sure VBScript makes only one replacement.

The SetIndexAt Function
Although VBScript lets you replace substrings in a string, it doesn't let you replace one character at a fixed position. You can use the Mid function to read the nth character in a string with code such as

MsgBox Mid(str, n, 1)

but no function exists to replace that character as a string. Thus, I created the SetIndexAt function, which has the syntax

SetIndexAt(str, pos, char)

where str specifies the string, pos specifies the position of the character you want to replace in that string, and char specifies the new character as a string (not ASCII code). For example, the code

path = "C:\mydir\mysubdir\myfile.ext"
MsgBox SetIndexAt(path, 9, "/")

tells VBScript to replace the ninth character in the string "C:\mydir\mysubdir\myfile.ext" with a slash.

Listing B contains the code for the SetIndexAt function. To create this function, I first declared and defined the function and declared the find, temp, and first variables. I then assigned values to these three variables. I set the temp variable to the string (in this example, "C:\mydir\mysubdir\myfile.ext"). I used the Mid function to extract only one character from the start position (9) of that string and assigned that character ("\") to the find variable. I used the Left function to extract 8 characters (9 - 1) from the left side of the string and assigned that substring ("C:\mydir") to the first variable. (I'll explain the role of this variable shortly.)

Next, I used this Replace function

Replace(temp, find, char, pos, 1)

to replace the character. When I substitute the example's values for the variables, the function reads

Replace("C:\mydir\mysubdir\myfile.ext", "\", "/", 9, _ 1)

In other words, starting from position 9 in the string "C:\mydir\mysubdir\myfile.ext", I'm replacing the first instance of a backslash with a slash. Because the start position is 9 and I'm making only one replacement, Replace truncates the portion of the string before the replaced character (i.e., truncates "C:\mydir"). The workaround for this truncating problem is simple. Before you use the Replace function, you save the part that the function will truncate and reattach that part after the replacement occurs. In this case, I already saved "C:\mydir" in the first variable, so now I just need to reattach it. The concatenation (&) operator in the line

temp = first & Replace(temp, find, char, pos, 1)

does just that.

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish