How to make Sentence Case
Posted on January 4, 2009
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | 'String Manipulation - How to make Sentence Case Public Function MakeSentenceCase(ByVal strString As String) As String Dim strText() As String Dim strTemp As String Dim I As Integer strText = Split(strString) For I = 0 To UBound(strText) If I = 0 Then strTemp = strTemp & StrConv(strText(0), vbProperCase) ElseIf Right$(strText(I - 1), 1) Like "[.!?]" Then strTemp = strTemp & StrConv(strText(I), vbProperCase) Else strTemp = strTemp & StrConv(strText(I), vbLowerCase) End If strTemp = strTemp & Space$(1) Next I MakeSentenceCase = strTemp End Function 'How to call this function: 'MSgBox MakeSentenceCase("thIs is A TesT. JUSt a tESt.") 'You will get: This is a test. Just a test. 'Another way: Private Function SentenceCase(ByVal sText As String) Dim b() As Byte, N As Long, bChange As Boolean b = sText bChange = True For N = 0 To UBound(b) Step 2 Select Case b(N) Case 33, 46, 58, 63 ' !.:? bChange = True Case 97 To 122 ' a-z If bChange Then b(N) = b(N) - 32: bChange = False Case 32 Case Else bChange = False End Select Next N SentenceCase = b End Function 'How to call this function: 'MSgBox SentenceCase("thIs is A TesT. JUSt a tESt.") 'You will get: ThIs is A TesT. JUSt a tESt. |