Created: March 1, 1995
Visual Basic® offers many functions that can be used to manipulate text strings. Using the LCase and UCase functions, you can change the characters in a string to all lowercase or all uppercase letters. This article shows how you can convert the first letter of each word in a string to a capital letter.
When you want to convert a string to all lowercase or all uppercase letters, you can use the LCase and UCase functions, respectively. LCase converts the text in the specified string to all lowercase characters, while UCase converts the text to all uppercase letters.
The Mid function can be used to examine a particular string within a larger string. You can use Mid to extract one or more characters from a larger string, manipulate the characters in some manner, and the old characters are replaced with the newly modified letters.
Another Visual Basic® function used to manipulate text strings is the InStr function, which can be used to find a specific character within a string.
By combining the InStr, Mid, and UCase functions, you can selectively convert parts of a string to uppercase letters.
The following Visual Basic program converts the sentence in the first text box so that each word is capitalized.
Sub Form_Load()
Text1.Text = "microsoft visual basic is a fun"
Text1.Text = Text1.Text + " programming language."
Text2.Text = CapAllWords(Text1.Text)
End Sub
Function CapAllWords(ByVal MyString As String) As String
Dim PosSpc As Integer
Mid(MyString, 1, 1) = UCase(Mid(MyString, 1, 1))
PosSpc = InStr(MyString, " ")
While PosSpc <> 0
Mid(MyString, PosSpc + 1, 1) = UCase(Mid(MyString, PosSpc + 1, 1))
PosSpc = InStr(PosSpc + 1, MyString, " ")
Wend
CapAllWords = MyString
End Function
When you execute this sample program, Visual Basic displays a lowercase string in the first text box. The first character of each word in this sentence is then converted to a capital letter. The converted string is shown in the second text box.