Results 1 to 9 of 9

Thread: [RESOLVED] vbProperCase

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2006
    Posts
    186

    Resolved [RESOLVED] vbProperCase

    I need to apply vbProperCase to just the first letter of every sentence in the string, and not to every word in the string:
    VB Code:
    1. myString = "thIs is A TesT. JUSt a tESt."
    2. myString = StrConv(myString, vbProperCase)
    I need myString to come out like this: "This is a test. Just a test.", and not like: "This Is A Test. Just A Test."
    Anybody knows how to do this?

  2. #2
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: vbProperCase

    This isn't bullet prooof...

    VB Code:
    1. Dim MyString As String
    2. Dim lngIndex As Long
    3. Dim bFoundEOS As Boolean
    4.  
    5. MyString = "thIs is A TesT. JUSt a tESt."
    6. MyString = LCase$(MyString)
    7.  
    8. MyString = UCase$(Left$(MyString, 1)) & Mid$(MyString, 2)
    9. For lngIndex = 2 To Len(MyString)
    10.     Select Case Mid$(MyString, lngIndex, 1)
    11.         Case ".", "!", "?"
    12.             bFoundEOS = True
    13.         Case Else
    14.             If Mid$(MyString, lngIndex, 1) <> " " Then
    15.                 If bFoundEOS Then
    16.                     MyString = VBA.Left$(MyString, lngIndex - 1) _
    17.                              & UCase$(Mid$(MyString, lngIndex, 1)) _
    18.                              & Mid$(MyString, lngIndex + 1)
    19.                     bFoundEOS = False
    20.                 End If
    21.             End If
    22.     End Select
    23. Next
    24. MsgBox MyString

  3. #3
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: vbProperCase

    What you want is sentence case and here's some code I found and modified that might do it (untested, sorry):

    VB Code:
    1. Function StrToSentenceCase(ByVal txt As String)
    2.   Dim txts As String, txtChr As String
    3.   Dim X As Long, Y As Long
    4.  
    5.   'Check the first 10 characters to get
    6.   'the first - incase the selection includes
    7.   'some spaces at the beginning.
    8.   For X = 1 To 10
    9.     txtChr = Mid$(txt, X, 1)
    10.     If txtChr Like "[a-zA-Z]" Then
    11.       Mid$(txt, X, 1) = UCase(txtChr)
    12.       Exit For
    13.     End If
    14.   Next X
    15.  
    16.   'Check each character to find the criteria (.!?)
    17.   'for the end of a sentence, then Capitalize the next letter.
    18.   For X = X To Len(txt)
    19.     txtChr = Mid$(txt, X, 1)
    20.     If txtChr Like "[.!?]" Then
    21.       For Y = X + 1 To X + 10
    22.         txtChr = Mid$(txt, Y, 1)
    23.         If txtChr Like "[a-zA-Z]" Then
    24.           Mid$(txt, Y, 1) = UCase(txtChr)
    25.           X = Y + 1
    26.           Exit For
    27.         End If
    28.       Next Y
    29.     End If
    30.   Next X
    31.  
    32.   StrToSentenceCase = txt
    33. End Function

    Original here:
    vbcity.com/forums/topic.asp?tid=17044

    Sorry, it's not great.

  4. #4
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: vbProperCase

    More untested code this time written by yours truly.

    VB Code:
    1. Function StrToSentenceCase(inputStr As String)
    2.   Dim buffer() As Byte: buffer = inputStr
    3.   Dim i As Long, isNewSentence As Boolean
    4.  
    5.   For i = 0 To UBound(buffer)
    6.     If (isNewSentence) Then
    7.       If (buffer(i) >= 97 And buffer(i) <= 122) Then _
    8.         buffer(i) = buffer(i) - 32
    9.      
    10.       isNewSentence = False
    11.     Else
    12.       If (buffer(i) >= 65 And buffer(i) <= 90) Then
    13.         buffer(i) = buffer(i) + 32
    14.       Else
    15.         isNewSentence = (buffer(i) = 33 Or buffer(i) = 46 Or buffer(i) = 63)
    16.       End If
    17.     End If
    18.   Next i
    19.  
    20.   StrToSentenceCase = buffer
    21. End Function

    That should hopefully be a bit faster. Let me know if it doesn't work. Unfortunately I dont have VB6 at the moment.
    Last edited by penagate; May 7th, 2006 at 09:36 AM.

  5. #5
    Frenzied Member cssriraman's Avatar
    Join Date
    Jun 2005
    Posts
    1,465

    Re: vbProperCase

    @penagate:

    "End If" is missing in Post #4 code and it converts everything to uppercase.
    CS

  6. #6
    Frenzied Member cssriraman's Avatar
    Join Date
    Jun 2005
    Posts
    1,465

    Re: vbProperCase

    Oops! I forget to tell you, Post #3 works fine.
    CS

  7. #7
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: vbProperCase

    Cool, would you be able to try 4 again? I fixed a bit

  8. #8
    Frenzied Member cssriraman's Avatar
    Join Date
    Jun 2005
    Posts
    1,465

    Re: vbProperCase

    This code will do the same thing:
    VB Code:
    1. Public Function MakeSentenceCase(ByVal strString As String) As String
    2.     Dim strText() As String
    3.     Dim strTemp As String
    4.     Dim I As Integer
    5.     strText = Split(strString)
    6.     For I = 0 To UBound(strText)
    7.         If I = 0 Then
    8.             strTemp = strTemp & StrConv(strText(0), vbProperCase)
    9.         ElseIf Right$(strText(I - 1), 1) Like "[.!?]" Then
    10.             strTemp = strTemp & StrConv(strText(I), vbProperCase)
    11.         Else
    12.             strTemp = strTemp & StrConv(strText(I), vbLowerCase)
    13.         End If
    14.         strTemp = strTemp & Space$(1)
    15.     Next I
    16.     MakeSentenceCase = strTemp
    17. End Function
    CS

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Mar 2006
    Posts
    186

    Re: vbProperCase

    I'm using code in post 8. Works great. Thank you guys.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width