|
-
May 7th, 2006, 07:40 AM
#1
Thread Starter
Addicted Member
[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:
myString = "thIs is A TesT. JUSt a tESt."
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?
-
May 7th, 2006, 07:59 AM
#2
Re: vbProperCase
This isn't bullet prooof...
VB Code:
Dim MyString As String
Dim lngIndex As Long
Dim bFoundEOS As Boolean
MyString = "thIs is A TesT. JUSt a tESt."
MyString = LCase$(MyString)
MyString = UCase$(Left$(MyString, 1)) & Mid$(MyString, 2)
For lngIndex = 2 To Len(MyString)
Select Case Mid$(MyString, lngIndex, 1)
Case ".", "!", "?"
bFoundEOS = True
Case Else
If Mid$(MyString, lngIndex, 1) <> " " Then
If bFoundEOS Then
MyString = VBA.Left$(MyString, lngIndex - 1) _
& UCase$(Mid$(MyString, lngIndex, 1)) _
& Mid$(MyString, lngIndex + 1)
bFoundEOS = False
End If
End If
End Select
Next
MsgBox MyString
-
May 7th, 2006, 08:04 AM
#3
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:
Function StrToSentenceCase(ByVal txt As String)
Dim txts As String, txtChr As String
Dim X As Long, Y As Long
'Check the first 10 characters to get
'the first - incase the selection includes
'some spaces at the beginning.
For X = 1 To 10
txtChr = Mid$(txt, X, 1)
If txtChr Like "[a-zA-Z]" Then
Mid$(txt, X, 1) = UCase(txtChr)
Exit For
End If
Next X
'Check each character to find the criteria (.!?)
'for the end of a sentence, then Capitalize the next letter.
For X = X To Len(txt)
txtChr = Mid$(txt, X, 1)
If txtChr Like "[.!?]" Then
For Y = X + 1 To X + 10
txtChr = Mid$(txt, Y, 1)
If txtChr Like "[a-zA-Z]" Then
Mid$(txt, Y, 1) = UCase(txtChr)
X = Y + 1
Exit For
End If
Next Y
End If
Next X
StrToSentenceCase = txt
End Function
Original here:
vbcity.com/forums/topic.asp?tid=17044
Sorry, it's not great.
-
May 7th, 2006, 08:22 AM
#4
Re: vbProperCase
More untested code this time written by yours truly.
VB Code:
Function StrToSentenceCase(inputStr As String)
Dim buffer() As Byte: buffer = inputStr
Dim i As Long, isNewSentence As Boolean
For i = 0 To UBound(buffer)
If (isNewSentence) Then
If (buffer(i) >= 97 And buffer(i) <= 122) Then _
buffer(i) = buffer(i) - 32
isNewSentence = False
Else
If (buffer(i) >= 65 And buffer(i) <= 90) Then
buffer(i) = buffer(i) + 32
Else
isNewSentence = (buffer(i) = 33 Or buffer(i) = 46 Or buffer(i) = 63)
End If
End If
Next i
StrToSentenceCase = buffer
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.
-
May 7th, 2006, 09:33 AM
#5
Re: vbProperCase
@penagate:
"End If" is missing in Post #4 code and it converts everything to uppercase.
-
May 7th, 2006, 09:34 AM
#6
Re: vbProperCase
Oops! I forget to tell you, Post #3 works fine.
-
May 7th, 2006, 09:36 AM
#7
Re: vbProperCase
Cool, would you be able to try 4 again? I fixed a bit
-
May 7th, 2006, 09:45 AM
#8
Re: vbProperCase
This code will do the same thing:
VB Code:
Public Function MakeSentenceCase(ByVal strString As String) As String
Dim strText() As String
Dim strTemp As String
Dim I As Integer
strText = Split(strString)
For I = 0 To UBound(strText)
If I = 0 Then
strTemp = strTemp & StrConv(strText(0), vbProperCase)
ElseIf Right$(strText(I - 1), 1) Like "[.!?]" Then
strTemp = strTemp & StrConv(strText(I), vbProperCase)
Else
strTemp = strTemp & StrConv(strText(I), vbLowerCase)
End If
strTemp = strTemp & Space$(1)
Next I
MakeSentenceCase = strTemp
End Function
-
May 8th, 2006, 03:08 PM
#9
Thread Starter
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|