Results 1 to 18 of 18

Thread: First letter on each sentence UCase

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2008
    Posts
    5

    First letter on each sentence UCase

    hi
    i need some help to make text in textbox change to first letter on first word in a sentence UCase.
    im a trying to do this: You input Text into a Text Box, you click a radio button, then you click a convert text cmd and it changes the text in the text box to first letter on first word in a sentence UCase.
    I have done first letter on each word upper case which was
    Code:
    Option Explicit
    
    Private Sub Command1_Click()
        MsgBox makeFirstUCase("hello i aM aaRon")
    End Sub
    
    
    Private Function makeFirstUCase(myStr As String) As String
        Dim strArr() As String
        Dim i As Integer
        
        strArr = Split(myStr, " ")
        For i = 0 To UBound(strArr)
            If strArr(i) <> vbNullString Then
                strArr(i) = UCase(Left$(strArr(i), 1)) & Right$(strArr(i), Len(strArr(i)) - 1)
            End If
            makeFirstUCase = makeFirstUCase & strArr(i) & " "
        Next
        
        
        
    End Function
    Please help

  2. #2
    Hyperactive Member
    Join Date
    Feb 2008
    Posts
    327

    Re: First letter on each sentence UCase

    if this is the result that you want Hello I Am Aaron then you may use this code
    vb Code:
    1. Private Function makeFirstUCase(myStr As String) As String
    2.     Dim strArr() As String
    3.     Dim i As Integer
    4.     strArr = Split(myStr, " ")
    5.     For i = 0 To UBound(strArr)
    6.         If strArr(i) <> vbNullString Then
    7.             strArr(i) = UCase(Left$(strArr(i), 1)) & LCase(Mid$(strArr(i), 2))
    8.         End If
    9.         makeFirstUCase = makeFirstUCase & strArr(i) & " "
    10.     Next
    11. End Function

  3. #3

    Thread Starter
    New Member
    Join Date
    Aug 2008
    Posts
    5

    Re: First letter on each sentence UCase

    thanks but i wanted the result of:
    that dog is very lazy. it should go for a run! why is it tired? to
    That dog is very lazy. It should go for a run! Why is it tired?
    Last edited by aaron1259; Aug 18th, 2008 at 03:05 AM.

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: First letter on each sentence UCase

    you need to search any character that can end sentance, followed by space, try like this
    vb Code:
    1. pos = instr(text1.text, ". ")
    2. do while pos > 0
    3.   text1.text = left(text1.text, pos +1) & ucase (mid(text1.text, pos +2, 1)) & mid(text1, pos +3)
    4.   pos = instr(pos +1, text1.text, ". ")
    5. loop
    followed by similar loops for ? ! and other possible sentence endings
    this will fail for any sentence /word enclosed in quotes, so you may need to test for quotes in each case, also words following an abbreviation may also be an issue
    eg Dr. or Mr., though in both those case capitalising the next letter is probably fine, but in some cases may not
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  5. #5
    Hyperactive Member
    Join Date
    Jun 2007
    Posts
    280

    Re: First letter on each sentence UCase

    You will probably have to check for acronyms too.
    Slower than a crippled Vista
    More buggy than a fresh XP install
    Look! Down the road, some 50 miles behind the drunken snail.
    It's Ubuntu!

  6. #6
    Hyperactive Member
    Join Date
    Feb 2008
    Posts
    327

    Re: First letter on each sentence UCase

    you need to first determine what could be the end of the sentence in your case it is "." & "!" & "?" .now u need to loop thru until u find these characters and convert the next letter after a space to ucase (proper sentence structure)
    try this code
    vb Code:
    1. Private Sub Command1_Click()
    2.  MsgBox convTxt("that dog is very lazy. it should go for a run! why is it tired? oh my god are u serious?")
    3.  
    4. End Sub
    5.  
    6. Private Function convTxt(strTxt As String) As String 'strTxt
    7.    
    8.     Dim strconst As String
    9.     'determine the character that could be the end of a sentence
    10.     strconst = ".!?"
    11.    
    12.     Dim strone As String
    13.     Dim y As Integer
    14.     Dim strTwo As String
    15.     Dim x As Integer
    16.     'go thru each character
    17.     For x = 1 To Len(strconst)
    18.         strTwo = Mid$(strconst, x, 1)
    19.         y = 0
    20.        
    21.         Do
    22.            
    23.             y = InStr(y + 1, strTxt, strTwo)
    24.             strone = Mid(strTxt, y + 2, 1)
    25.             If y = 0 Then Exit Do
    26.            
    27.             'convert to ucase
    28.             strTxt = Replace(strTxt, strTwo & " " & strone, strTwo & " " & UCase(strone))
    29.         Loop
    30.     Next
    31.     'convert first letter of the sentence to ucase
    32.     convTxt = UCase(Left$(strTxt, 1)) & Mid(strTxt, 2)
    33. End Function

  7. #7
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,969

    Re: First letter on each sentence UCase

    Quote Originally Posted by aaron1259
    thanks but i wanted the result of:
    that dog is very lazy. it should go for a run! why is it tired? to
    That dog is very lazy. It should go for a run! Why is it tired?
    Maybe:

    MsgBox StrConv("that dog is very lazy. it should go for a run! why is it tired?", vbProperCase)

  8. #8
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: First letter on each sentence UCase

    Quote Originally Posted by TysonLPrice
    Maybe:

    MsgBox StrConv("that dog is very lazy. it should go for a run! why is it tired?", vbProperCase)
    vbProperCase option will capitalize first letter of each word:
    "That Dog Is Very Lazy. It Should Go For A Run! Why Is It Tired?"

    There must be something like convert to "Sentence case" in MS-Word.
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  9. #9
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: First letter on each sentence UCase

    Quote Originally Posted by anhn
    There must be something like convert to "Sentence case" in MS-Word.
    There is.

    Short of that, here's another version of a sentence case function I posted last year:
    Code:
    Public Function SentenceCase(pstrText As String) As String
        Dim i As Long
        Dim bytArray() As Byte
        Dim blnUpper As Boolean
        
        blnUpper = True
        bytArray = StrConv(pstrText, vbFromUnicode)
        For i = 0 To UBound(bytArray)
            Select Case bytArray(i)
                Case 33, 46, 63 ' exclamation point, period or question mark
                    blnUpper = True
                Case 65 To 90, 97 To 122
                    If blnUpper Then
                        bytArray(i) = Asc(UCase$(Chr$(bytArray(i))))
                        blnUpper = False
                    End If
            End Select
        Next
        SentenceCase = StrConv(bytArray, vbUnicode)
        Erase bytArray
    End Function

  10. #10
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    Re: First letter on each sentence UCase

    Aaron, this code seems to have some promise. Build a form with a command button and a text box.
    Code:
    Dim MyString As String
    Private Sub Command1_Click()
    Mid$(MyString, 1, 1) = UCase(Mid$(MyString, 1, 1)) ' Cap first letter
    Dim Pointer As Long
    Do While Pointer < Len(MyString)
        Pointer = Pointer + 1
        Select Case Asc(Mid$(MyString, Pointer, 1))
        Case 33, 46, 63 ' Found end of a sentence
            If Pointer < Len(MyString) Then
                Pointer = Pointer + 1
                Do While Mid$(MyString, Pointer, 1) = " " And Pointer < Len(MyString)
                    Pointer = Pointer + 1
                Loop
                ' Cap first letter of next sentence
                Mid$(MyString, Pointer, 1) = UCase(Mid$(MyString, Pointer, 1))
            End If
        Case Else
        End Select
    Loop
    Text1.Text = MyString
    End Sub
    
    Private Sub Form_Load()
    ' Sample collection of sentences
    MyString = "cap me... please cap me? now cap me! Don't have to cap me; OK as is!..."
    Text1.Text = MyString
    End Sub
    Last edited by Code Doc; Aug 18th, 2008 at 03:21 PM.
    Doctor Ed

  11. #11
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    SentenceCase: Capitalize first letter of each sentence

    This is my version. I want to make it as close as possible to an MS-Word's method, it also covers Unicode characters.

    Moved to CodeBank.
    Last edited by anhn; Aug 22nd, 2008 at 07:54 PM.
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  12. #12
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    Re: First letter on each sentence UCase

    Terrific, anhn, but I notice that you are starting by converting all text to lower case before you start and that would seem to add a bit of complexity, thus turning the code into somewhat of an expert grammar/spell checker.

    To me, that means that if the user wanted caps here and there, such as VB coded variables (e.g., SentenceCase above) that these caps would be eliminated. Please corect me if wrong.

    I really do like the way this code handles the non-breaking space, wordwrap, and when the sentence starts immediately on a line after a hard return. I forgot about those instances.
    Doctor Ed

  13. #13
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: First letter on each sentence UCase

    Quote Originally Posted by Code Doc
    Terrific, anhn, but I notice that you are starting by converting all text to lower case before you start and that would seem to add a bit of complexity, thus turning the code into somewhat of an expert grammar/spell checker.

    To me, that means that if the user wanted caps here and there, such as VB coded variables (e.g., SentenceCase above) that these caps would be eliminated. Please corect me if wrong.
    Any way we use will have some limitations. Without converting to lowercase first such as in case of all UPPERCASE, after calling SentenCase() the text is still in all UPPERCASE. That cannot be called Sentence Case.
    Check to see how MS-Word change case to Sentence Case.
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  14. #14
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: First letter on each sentence UCase

    If you want to emulate Word, don't forget about the ellipsis. (...) It appears that Word leaves any text after an ellipsis in the same case it started. This won't work if you convert everything to lower case to start.

    EDIT: The behavior changes if the text is all uppercase to start. The "leave it as it was" only applies to mixed-case text.

  15. #15
    Hyperactive Member Davadvice's Avatar
    Join Date
    Apr 2007
    Location
    Glasgow (Scotland)
    Posts
    440

    Re: First letter on each sentence UCase

    This may be off the wall! But can you not exploit what is used by MS word as this deals with all these issues already ?
    This is Blank

  16. #16
    Addicted Member
    Join Date
    May 2006
    Location
    New Romney, Kent, UK
    Posts
    232

    Re: First letter on each sentence UCase

    Davadvice - You mean an API that Word uses?

  17. #17
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: First letter on each sentence UCase

    My Version...

    Try This

    Code:
    Sub ConvertToSentenceCase()
    Dim TestString As String, strg2 As String
    TestString = "wake me up before you go go. i want to have my breakfast! don't forget."
    
    'test the 1st 10 chars to get the first - incase the selection includes
    'some spaces at the beginning.
    For X = 1 To 10
      strg2 = Mid(TestString, X, 1)
      If strg2 Like "[a-zA-Z]" Then
        Mid(TestString, X, 1) = UCase(strg2)
        Exit For
      End If
    Next X
    
    'test criteria (.!?) for the end of a sentence, then Capitalize the next letter.
    For X = X To Len(TestString)
      strg2 = Mid(TestString, X, 1)
      If strg2 Like "[.!?]" Then
        For Y = X + 1 To X + 10
          strg2 = Mid(TestString, Y, 1)
          If strg2 Like "[a-zA-Z]" Then
            Mid(TestString, Y, 1) = UCase(strg2)
            X = Y + 1
            Exit For
          End If
        Next Y
      End If
    Next X
    
    'This will give you the result
    MsgBox TestString
    
    End Sub
    Last edited by Siddharth Rout; Aug 19th, 2008 at 04:10 PM.
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  18. #18
    Hyperactive Member Davadvice's Avatar
    Join Date
    Apr 2007
    Location
    Glasgow (Scotland)
    Posts
    440

    Re: First letter on each sentence UCase

    Quote Originally Posted by GettinBetter
    Davadvice - You mean an API that Word uses?
    yeah, if the API will allow access to the area responsable for performing this action in Word then is there no way this can be used ?

    I know that in Access there is the function to auto spell check then i asume Words API will allow a call to pass a string and return the formated text.

    thus no need to reinvent the wheel as such ?

    thanks
    David
    This is Blank

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