|
-
Oct 30th, 2006, 11:17 AM
#1
Thread Starter
Hyperactive Member
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:
Public Function strSentenceCapitals(strString As String, Optional spaceNum As Integer = 1, Optional withDoEvents As Boolean = False) As String
If strString = "" Then Exit Function
'Check for all end-of-sentence punctuation
If Not InStrB(strString, ". ") > 0 Then
If Not InStrB(strString, "? ") > 0 Then
If Not InStrB(strString, "! ") > 0 Then
Exit Function
End If
End If
End If
If spaceNum < 1 Then spaceNum = 1
If spaceNum > 1 Then spaceNum = 2
Dim searchStrTxt As Integer
Dim strCmpare As String
Dim strRplc As String
Dim strModified As String
Dim strFirstChr As String
If withDoEvents = True Then
GoTo doFreeProc
Else
GoTo doHoldProc
End If
Exit Function
'Do with DoEvents
doFreeProc:
strModified = strString
strFirstChr = UCase(Mid$(strModified, 1, 1))
strModified = strFirstChr & Mid$(strModified, 2)
If spaceNum = 1 Then
'Search for single-spaced punctuation
For searchStrTxt = 1 To Len(strString) - 1
DoEvents
strCmpare = Mid$(strModified, searchStrTxt, 2)
If strCmpare = ". " Then
strRplc = Mid$(strString, searchStrTxt, 3)
strModified = Replace$(strModified, strRplc, UCase$(strRplc))
End If
If strCmpare = "? " Then
strRplc = Mid$(strString, searchStrTxt, 3)
strModified = Replace$(strModified, strRplc, UCase$(strRplc))
End If
If strCmpare = "! " Then
strRplc = Mid$(strString, searchStrTxt, 3)
strModified = Replace$(strModified, strRplc, UCase$(strRplc))
End If
Next searchStrTxt
Else
'Search for double-spaced punctuation
For searchStrTxt = 1 To Len(strString) - 1
DoEvents
strCmpare = Mid$(strModified, searchStrTxt, 2)
If strCmpare = ". " Then
strRplc = Mid$(strString, searchStrTxt, 3)
strModified = Replace$(strModified, strRplc, UCase$(strRplc))
End If
If strCmpare = "? " Then
strRplc = Mid$(strString, searchStrTxt, 3)
strModified = Replace$(strModified, strRplc, UCase$(strRplc))
End If
If strCmpare = "! " Then
strRplc = Mid$(strString, searchStrTxt, 3)
strModified = Replace$(strModified, strRplc, UCase$(strRplc))
End If
Next searchStrTxt
End If
'Assign new string
strSentenceCapitals = strModified
Exit Function
'Do without DoEvents
doHoldProc:
strModified = strString
strFirstChr = UCase(Mid$(strModified, 1, 1))
strModified = strFirstChr & Mid$(strModified, 2)
If spaceNum = 1 Then
'Search for single-spaced punctuation
For searchStrTxt = 1 To Len(strString) - 1
'DoEvents
strCmpare = Mid$(strModified, searchStrTxt, 3)
If strCmpare = ". " Then
strRplc = Mid$(strString, searchStrTxt, 4)
strModified = Replace$(strModified, strRplc, UCase$(strRplc))
End If
If strCmpare = "? " Then
strRplc = Mid$(strString, searchStrTxt, 4)
strModified = Replace$(strModified, strRplc, UCase$(strRplc))
End If
If strCmpare = "! " Then
strRplc = Mid$(strString, searchStrTxt, 4)
strModified = Replace$(strModified, strRplc, UCase$(strRplc))
End If
Next searchStrTxt
Else
'Search for double-spaced punctuation
For searchStrTxt = 1 To Len(strString) - 1
'DoEvents
strCmpare = Mid$(strModified, searchStrTxt, 3)
If strCmpare = ". " Then
strRplc = Mid$(strString, searchStrTxt, 4)
strModified = Replace$(strModified, strRplc, UCase$(strRplc))
End If
If strCmpare = "? " Then
strRplc = Mid$(strString, searchStrTxt, 4)
strModified = Replace$(strModified, strRplc, UCase$(strRplc))
End If
If strCmpare = "! " Then
strRplc = Mid$(strString, searchStrTxt, 4)
strModified = Replace$(strModified, strRplc, UCase$(strRplc))
End If
Next searchStrTxt
End If
'Assign new string
strSentenceCapitals = strModified
End Function
You would call it like this, for single-spaced sentences(ie. "Hi. My name is Joe"):
VB Code:
Text1.Text = strSentenceCapitals(Text1.Text)
'Or...
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:
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!
-
Oct 30th, 2006, 01:20 PM
#2
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:
Private Sub Form_Load()
Debug.Print SentenceCase("this is a test. is it a test? yes it is! a test.")
End Sub
Private Function SentenceCase(ByVal sText As String) 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
Last edited by bushmobile; Oct 30th, 2006 at 06:31 PM.
-
Oct 30th, 2006, 06:28 PM
#3
Thread Starter
Hyperactive Member
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 =)
-
Oct 30th, 2006, 07:21 PM
#4
Re: Capitalize beginning of sentences(single and double spaced)
 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
 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)
 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.
 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|