Results 1 to 4 of 4

Thread: Capitalize beginning of sentences(single and double spaced)

  1. #1

    Thread Starter
    Hyperactive Member BrendanDavis's Avatar
    Join Date
    Oct 2006
    Location
    Florida
    Posts
    492

    Capitalize beginning of sentences(single and double spaced)

    Here is a function I just wrote that will capitalize the first letter of each new sentence. It looks for the ending punctuation followed by a space(or two, if you specify it to do so via "spaceNum" integer = 2 instead of 1), and then finds the character afterwards and makes it UCase$. It also has the option of(if you have a huge string to work with) do it with DoEvents, letting you do other tasks, or without it(default is without it):

    VB Code:
    1. Public Function strSentenceCapitals(strString As String, Optional spaceNum As Integer = 1, Optional withDoEvents As Boolean = False) As String
    2.  
    3.     If strString = "" Then Exit Function
    4.    
    5.     'Check for all end-of-sentence punctuation
    6.     If Not InStrB(strString, ". ") > 0 Then
    7.         If Not InStrB(strString, "? ") > 0 Then
    8.             If Not InStrB(strString, "! ") > 0 Then
    9.                 Exit Function
    10.             End If
    11.         End If
    12.     End If
    13.    
    14.    
    15.     If spaceNum < 1 Then spaceNum = 1
    16.     If spaceNum > 1 Then spaceNum = 2
    17.    
    18.    
    19.         Dim searchStrTxt As Integer
    20.         Dim strCmpare As String
    21.         Dim strRplc As String
    22.         Dim strModified As String
    23.         Dim strFirstChr As String
    24.    
    25.    
    26.         If withDoEvents = True Then
    27.             GoTo doFreeProc
    28.         Else
    29.             GoTo doHoldProc
    30.         End If
    31.        
    32.     Exit Function
    33.  
    34.  
    35.  
    36. 'Do with DoEvents
    37. doFreeProc:
    38.    
    39.     strModified = strString
    40.     strFirstChr = UCase(Mid$(strModified, 1, 1))
    41.     strModified = strFirstChr & Mid$(strModified, 2)
    42.    
    43.     If spaceNum = 1 Then
    44.         'Search for single-spaced punctuation
    45.         For searchStrTxt = 1 To Len(strString) - 1
    46.             DoEvents
    47.             strCmpare = Mid$(strModified, searchStrTxt, 2)
    48.                 If strCmpare = ". " Then
    49.                     strRplc = Mid$(strString, searchStrTxt, 3)
    50.                     strModified = Replace$(strModified, strRplc, UCase$(strRplc))
    51.                 End If
    52.                
    53.                 If strCmpare = "? " Then
    54.                     strRplc = Mid$(strString, searchStrTxt, 3)
    55.                     strModified = Replace$(strModified, strRplc, UCase$(strRplc))
    56.                 End If
    57.                
    58.                 If strCmpare = "! " Then
    59.                     strRplc = Mid$(strString, searchStrTxt, 3)
    60.                     strModified = Replace$(strModified, strRplc, UCase$(strRplc))
    61.                 End If
    62.         Next searchStrTxt
    63.     Else
    64.         'Search for double-spaced punctuation
    65.         For searchStrTxt = 1 To Len(strString) - 1
    66.             DoEvents
    67.             strCmpare = Mid$(strModified, searchStrTxt, 2)
    68.                 If strCmpare = ". " Then
    69.                     strRplc = Mid$(strString, searchStrTxt, 3)
    70.                     strModified = Replace$(strModified, strRplc, UCase$(strRplc))
    71.                 End If
    72.                
    73.                 If strCmpare = "? " Then
    74.                     strRplc = Mid$(strString, searchStrTxt, 3)
    75.                     strModified = Replace$(strModified, strRplc, UCase$(strRplc))
    76.                 End If
    77.                
    78.                 If strCmpare = "! " Then
    79.                     strRplc = Mid$(strString, searchStrTxt, 3)
    80.                     strModified = Replace$(strModified, strRplc, UCase$(strRplc))
    81.                 End If
    82.         Next searchStrTxt
    83.     End If
    84.        
    85.         'Assign new string
    86.         strSentenceCapitals = strModified
    87.    
    88.     Exit Function
    89.  
    90.  
    91.  
    92. 'Do without DoEvents
    93. doHoldProc:
    94.    
    95.     strModified = strString
    96.     strFirstChr = UCase(Mid$(strModified, 1, 1))
    97.     strModified = strFirstChr & Mid$(strModified, 2)
    98.    
    99.         If spaceNum = 1 Then
    100.         'Search for single-spaced punctuation
    101.         For searchStrTxt = 1 To Len(strString) - 1
    102.             'DoEvents
    103.             strCmpare = Mid$(strModified, searchStrTxt, 3)
    104.                 If strCmpare = ".  " Then
    105.                     strRplc = Mid$(strString, searchStrTxt, 4)
    106.                     strModified = Replace$(strModified, strRplc, UCase$(strRplc))
    107.                 End If
    108.                
    109.                 If strCmpare = "?  " Then
    110.                     strRplc = Mid$(strString, searchStrTxt, 4)
    111.                     strModified = Replace$(strModified, strRplc, UCase$(strRplc))
    112.                 End If
    113.                
    114.                 If strCmpare = "!  " Then
    115.                     strRplc = Mid$(strString, searchStrTxt, 4)
    116.                     strModified = Replace$(strModified, strRplc, UCase$(strRplc))
    117.                 End If
    118.         Next searchStrTxt
    119.     Else
    120.         'Search for double-spaced punctuation
    121.         For searchStrTxt = 1 To Len(strString) - 1
    122.             'DoEvents
    123.             strCmpare = Mid$(strModified, searchStrTxt, 3)
    124.                 If strCmpare = ".  " Then
    125.                     strRplc = Mid$(strString, searchStrTxt, 4)
    126.                     strModified = Replace$(strModified, strRplc, UCase$(strRplc))
    127.                 End If
    128.                
    129.                 If strCmpare = "?  " Then
    130.                     strRplc = Mid$(strString, searchStrTxt, 4)
    131.                     strModified = Replace$(strModified, strRplc, UCase$(strRplc))
    132.                 End If
    133.                
    134.                 If strCmpare = "!  " Then
    135.                     strRplc = Mid$(strString, searchStrTxt, 4)
    136.                     strModified = Replace$(strModified, strRplc, UCase$(strRplc))
    137.                 End If
    138.         Next searchStrTxt
    139.     End If
    140.        
    141.         'Assign new string
    142.         strSentenceCapitals = strModified
    143.  
    144. End Function


    You would call it like this, for single-spaced sentences(ie. "Hi. My name is Joe"):


    VB Code:
    1. Text1.Text = strSentenceCapitals(Text1.Text)
    2. 'Or...
    3. Text1.Text = strSentenceCapitals(Text1.Text, 1)

    The "1" specifies the number of spaces between sentences. It defaults at "1" so you don't really have to specify it if you're looking for single-spaced sentences. To look for double-space sentences, replace the "1" with "2" or any number higher than 1. The function will auto set the value to "2" if you specify an integer higher than 2, that way it's not looking for a 5-spaced sentence if you mis-type it.

    Also, to do it with DoEvents(to allow activity while the function is running), you would call it like so:

    VB Code:
    1. Text1.Text = strSentenceCapitals(Text1.Text, 1, True)

    The default is "False"(ie. Do without "DoEvents") so you'd have to specify it as "True" if you want to use DoEvents. If not, you don't have to specify and you can call the function either of the 2 above ways specified.


    Just thought this simple function might help some folks out ;D

    Enjoy!

  2. #2
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Capitalize beginning of sentences(single and double spaced)

    that code is insane, you've copied the whole procedure for the sake of commenting out two lines! why not just use an If statement?

    the CodeBank is intended for exemplar code, rather than just functional code - your code may do the job, but it doesn't present the best way of tackling the problem - and certainly doesn't present a good example of how to write professional code.

    What you're doing can be achieved in far fewer lines, and much more quickly. This being one possible way:
    VB Code:
    1. Private Sub Form_Load()
    2.     Debug.Print SentenceCase("this is a test. is it a test? yes it is! a test.")
    3. End Sub
    4.  
    5. Private Function SentenceCase(ByVal sText As String) As String
    6.     Dim b() As Byte, N As Long, bChange As Boolean
    7.     b = sText
    8.     bChange = True
    9.     For N = 0 To UBound(b) Step 2
    10.         Select Case b(N)
    11.             Case 33, 46, 58, 63  ' !.:?
    12.                 bChange = True
    13.             Case 97 To 122 ' a-z
    14.                 If bChange Then b(N) = b(N) - 32: bChange = False
    15.             Case 32
    16.             Case Else
    17.                 bChange = False
    18.         End Select
    19.     Next N
    20.     SentenceCase = b
    21. End Function

  3. #3

    Thread Starter
    Hyperactive Member BrendanDavis's Avatar
    Join Date
    Oct 2006
    Location
    Florida
    Posts
    492

    Re: Capitalize beginning of sentences(single and double spaced)

    I wrote it how I understood it, and presented it upon completion. I have a habit of skewing my code and reducing it down a lot, but only over time. What I wrote will get the job done, as you say. And it does so quickly and with more options than the code you presented. While your code may be shorter and more practical, it isn't any faster in the grand scheme of things. So why does it matter, really?

    True, I could have made the code MUCH smaller(in fact, I already did ;D) by not copying the procedure several times. But like I said, it still does the job and does so quite nicely and quickly, even if it does leave room for improvement.

    I may not be the best coder out there, especially being that I've been away from all forms of programming for more than 6 years until just recently(about 3 weeks ago or so), but that doesn't mean that something I wrote is useless just because there is another way to do it. Each person is different, and they're going to write their code in whatever way they understand it best AS WELL AS whatever way works for them/what they're doing.

    No harm in that =)

  4. #4
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Capitalize beginning of sentences(single and double spaced)

    Quote Originally Posted by BrendanDavis
    What I wrote will get the job done, as you say. And it does so quickly and with more options than the code you presented.
    what options? the number of spaces thing is irrelevant in my code - and if you want the DoEvents then it's just one extra line

    Quote Originally Posted by BrendanDavis
    While your code may be shorter and more practical, it isn't any faster in the grand scheme of things. So why does it matter, really?
    It is much, much faster.

    Some quick timings (compiled) reveal that for a short string (the one i used in post #2) my code was ~8 times faster.
    For a string 32x that length it was ~19 times faster
    For a string 32768x that length, well mine took ~80 milliseconds and i gave up waiting for yours after 5 minutes.

    someone could still get much faster results than my code if they were willing to put the effort in.

    also your code doesn't even work without modification (you've cut and pasted the wrong sections)

    Quote Originally Posted by BrendanDavis
    True, I could have made the code MUCH smaller(in fact, I already did ;D) by not copying the procedure several times. But like I said, it still does the job and does so quite nicely and quickly, even if it does leave room for improvement.
    if you knew that you could have done it better then why not wait till you've improved it - the code in the CodeBank is meant to set an example.

    Quote Originally Posted by BrendanDavis
    I may not be the best coder out there, especially being that I've been away from all forms of programming for more than 6 years until just recently(about 3 weeks ago or so), but that doesn't mean that something I wrote is useless just because there is another way to do it. Each person is different, and they're going to write their code in whatever way they understand it best AS WELL AS whatever way works for them/what they're doing.

    No harm in that =)
    but there is harm - the code is slow, bloated and advocates the use of bad programming logic (GoTo) and redundacy (If withDoEvents = True Then) - people use the codebank for examples of good code that they can learn from. This isn't good code, and needs to be highlighted as such.

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