Library code snippets
Proper Case Function
This neat function capitalizes the first characters of each word setting, and
turns all other characters to lowercase.
Public Function ProperCase(strText As String) As String 'Capitalize all
first characters of the input string 'and set all the others to lowercase
On Error GoTo Err_ProperCase
Dim I As Integer, intLen As Integer, strTemp As String, strFinal
As String
Dim isSpace As Boolean
strTemp = LCase(Trim(strText))
intLen = Len(Trim(strText))
strFinal = String(intLen, " ")
isSpace = True
For I = 1 To intLen
If Mid(strTemp, I, 1) = Chr(32) Then
strFinal =
Mid(strFinal, 1, I - 1) & Chr(32) & Mid(strFinal, I + 1)
isSpace =
True
ElseIf isSpace Then
strFinal =
Mid(strFinal, 1, I - 1) & UCase(Mid(strTemp, I, 1)) & Mid(strFinal, I
+ 1)
isSpace =
False
Else
strFinal =
Mid(strFinal, 1, I - 1) & Mid(strTemp, I, 1) & Mid(strFinal, I + 1)
End If
Next I
ProperCase = strFinal
Exit_ProperCase:
Exit Function
Err_ProperCase:
MsgBox Err.Description & vbCrLf & strText, vbInformation,
"ProperCase function"
End Function
Related articles
Related discussion
-
Run-time error '91'
by converter2009 (1 replies)
-
VB6 Runtime error 381 subsript out of range Error
by Uncle (2 replies)
-
passing and reading parameters from using Shell
by jigartoliya (0 replies)
-
Convert C++ code to VB6
by mawcot (4 replies)
-
listbox scrollbar
by Dennijr (10 replies)
Related podcasts
-
Christian Beauclair
14 mai 2008 (�mission #0074) ::.Christian Beauclair: Stratégies de migration VB6 vers .NET Nous discutons avec Christian Beauclair des stratégies de migration VB6 vers .NET. Entre autres, nous discutons comment utiliser le "VB 6 Code Advisor" et le "Interop Forms Toolkit" pour ajouter la puiss...
.net equivalent:
System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase()
!--removed tag-->Simplest One.
Text1.Text = StrConv(Text1.Text, vbProperCase)
Private Function FirstLetterCaps(str As String) As String
Dim ExtractWord, NewStr As String
Dim position As Integer
position = InStr(str, " ")
While position
ExtractWord = LCase$(Left$(str, position))
NewStr = NewStr & UCase$(Left$(ExtractWord, 1)) & Mid$(ExtractWord, 2)
str = Right$(str, Len(str) - position)
position = InStr(str, " ")
Wend
ExtractWord = LCase$(str)
NewStr = NewStr & UCase$(Left$(ExtractWord, 1)) & Mid$(ExtractWord, 2)
FirstLetterCaps = NewStr
End Function
Above function is called wth str argument passed to it. First word of the str is extracted by using Left$ function and changed to LCase for sure. Then First char of the extracted word is converted to UCase while remaining are retained as Lcase, and this word is added to Newstr. The str is then deducted of the first word using Right$ function. Position is moved to next space location. As the loop so performed, the Newstr continuously accumulates words while str looses them until str is left with last word whose first char is finally converted to UCase and added to Newstr. The Newstr thus is formed with First Chars UCase which is then returned to calling procedure.
Note: With VB6, StrConv(string, vbProperCase) function performs the same task in a single line of code with effortless ease.
D
This thread is for discussions of Proper Case Function.