Home > How-To Library > String Manipulation

How to make Sentence Case

**************************************************************** * © 2007 CodeItBetter http://www.codeitbetter.com * * This notice MUST stay intact for legal use * ****************************************************************
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.

If you would like to submit your code here please us. Do not forget to mention your name. We are always thankful to each and everyone of you who submitted their code here.