|
-
Aug 18th, 2008, 01:59 AM
#1
Thread Starter
New Member
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
-
Aug 18th, 2008, 02:43 AM
#2
Hyperactive Member
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:
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)) & LCase(Mid$(strArr(i), 2))
End If
makeFirstUCase = makeFirstUCase & strArr(i) & " "
Next
End Function
-
Aug 18th, 2008, 02:54 AM
#3
Thread Starter
New Member
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.
-
Aug 18th, 2008, 03:42 AM
#4
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:
pos = instr(text1.text, ". ") do while pos > 0 text1.text = left(text1.text, pos +1) & ucase (mid(text1.text, pos +2, 1)) & mid(text1, pos +3) pos = instr(pos +1, text1.text, ". ") 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
-
Aug 18th, 2008, 03:46 AM
#5
Hyperactive Member
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!
-
Aug 18th, 2008, 04:48 AM
#6
Hyperactive Member
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:
Private Sub Command1_Click() MsgBox convTxt("that dog is very lazy. it should go for a run! why is it tired? oh my god are u serious?") End Sub Private Function convTxt(strTxt As String) As String 'strTxt Dim strconst As String 'determine the character that could be the end of a sentence strconst = ".!?" Dim strone As String Dim y As Integer Dim strTwo As String Dim x As Integer 'go thru each character For x = 1 To Len(strconst) strTwo = Mid$(strconst, x, 1) y = 0 Do y = InStr(y + 1, strTxt, strTwo) strone = Mid(strTxt, y + 2, 1) If y = 0 Then Exit Do 'convert to ucase strTxt = Replace(strTxt, strTwo & " " & strone, strTwo & " " & UCase(strone)) Loop Next 'convert first letter of the sentence to ucase convTxt = UCase(Left$(strTxt, 1)) & Mid(strTxt, 2) End Function
-
Aug 18th, 2008, 05:06 AM
#7
Re: First letter on each sentence UCase
 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)
-
Aug 18th, 2008, 05:26 AM
#8
Re: First letter on each sentence UCase
 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.
-
Aug 18th, 2008, 07:03 AM
#9
Re: First letter on each sentence UCase
 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
-
Aug 18th, 2008, 10:01 AM
#10
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
-
Aug 18th, 2008, 08:27 PM
#11
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.
-
Aug 19th, 2008, 07:20 AM
#12
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.
-
Aug 19th, 2008, 07:34 AM
#13
Re: First letter on each sentence UCase
 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.
-
Aug 19th, 2008, 07:53 AM
#14
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.
-
Aug 19th, 2008, 07:55 AM
#15
Hyperactive Member
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 ?
-
Aug 19th, 2008, 03:26 PM
#16
Addicted Member
Re: First letter on each sentence UCase
Davadvice - You mean an API that Word uses?
-
Aug 19th, 2008, 04:02 PM
#17
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
-
Aug 21st, 2008, 08:33 AM
#18
Hyperactive Member
Re: First letter on each sentence UCase
 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
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
|