Atomic Shrimp
Humanoid lifeform
- Local time
- Today, 09:21
- Joined
- Jun 16, 2000
- Messages
- 1,954
I had a large table of addresses and another of product details that were entirely in upper case; I needed to convert these to proper case, but the VB propercase function isn't quite clever enough;
[*]MCTAVISH should become McTavish but would actually become Mctavish and the customer would get angry.
[*]The 'OF' in 'KINGDOM OF BUTTER' would be captialised, when we wanted 'Kingdom of Butter'
[*]Postcodes; don't even talk to me about postcodes!
So I bashed out a quick case conversion function that does it very nearly correctly; there will always be an element of manual post-editing, but this function minimises it.
Enjoy
[*]MCTAVISH should become McTavish but would actually become Mctavish and the customer would get angry.
[*]The 'OF' in 'KINGDOM OF BUTTER' would be captialised, when we wanted 'Kingdom of Butter'
[*]Postcodes; don't even talk to me about postcodes!
So I bashed out a quick case conversion function that does it very nearly correctly; there will always be an element of manual post-editing, but this function minimises it.
Code:
Public Function SmartCase(ConvertStr As String) As String
Dim mPointer As Integer
Dim mletter As Integer
Dim mtemploop As Integer
Dim mWord As String
Dim mchar As String
If ConvertStr = "" Then
SmartCase = " "
Exit Function
End If
mPointer = 0
'loop through entire string
Do
mWord = ""
'loop to get the next word
Do
mPointer = mPointer + 1
mWord = mWord + Mid(ConvertStr, mPointer, 1)
Loop Until (Mid(ConvertStr, mPointer, 1) = " ") Or (Mid(ConvertStr, mPointer, 1) = "-") Or (mPointer > Len(ConvertStr))
'process the word:
'Default word conversion:
mletter = 0
Do
mletter = mletter + 1
If InStr("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", Mid(mWord, mletter, 1)) > 0 Then
mWord = (Mid(mWord, 1, mletter - 1)) & UCase(Mid(mWord, mletter, 1)) & LCase(Mid(mWord, mletter + 1, Len(mWord) - 1))
mletter = Len(mWord) + 1
End If
Loop Until mletter > Len(mWord)
'Conditional conversions:
'Celts Mc~ and O'~
If (Mid(mWord, 1, 2) = "Mc") Or (Mid(mWord, 2, 1) = "'") Then
mWord = UCase(Mid(mWord, 1, 1)) & LCase(Mid(mWord, 2, 1)) & UCase(Mid(mWord, 3, 1)) & LCase(Mid(mWord, 4, Len(mWord) - 2))
'(capitalise first and third letter, but not the rest)
End If
'Small words - do not capitalise at all unless it is the first word in the string
If (InStr("A-An-And-At-In-Is-Or-The-Of-", Trim(mWord)) > 0) And ((mPointer - Len(mWord)) > 0) Then
'(hyphens for Jack-in-the-Box - NB: you can customise this list)
mWord = LCase(mWord)
End If
'Common abbreviations - capitalise entirely
If InStr("Pb-Hb-Whs-", Trim(mWord)) > 0 Then
'(hyphens as above NB: my abbreviations are for Paperback, Hardback and Warehouse, but you can customise them)
mWord = UCase(mWord)
End If
'Words containing a numeric character - capitalise entirely
For mtemploop = 0 To 9
If InStr(mWord, Str(mtemploop)) > 0 Then
mWord = UCase(mWord)
End If
Next
SmartCase = SmartCase & mWord
Loop Until mPointer > Len(ConvertStr)
End Function
Enjoy