Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

String conversion problem

Status
Not open for further replies.

mark1110

Programmer
Apr 20, 2005
85
US
Hi,

I have a VB6 application that takes a field and formats it using the StrConv function, i.e, "john smith" becomes "John Smith". The problem I am having is if I have a name like "Ronald McDonald" and use the StrConv function I get "Ronald Mcdonald" instead of "Ronald McDonald" Is there a easy way to keep the capital letter in the last name?

Thanks,

Mark
 
I can't think of any. However if this is really important (personally I'm glad when anyone bothers to SPELL my name right, let alone get casing correct) you could write a custom ProperCase() conversion function that uses a table of prefixes to catch the most common scenarios.

This can never be 100%, since some might use Mcdonald intentionally. Then changing it to McDonald would be wrong.
 
Public Function myProperCase(str As String) As String
Dim aStr() As String
Dim vStr As Variant
str = Trim(str)
aStr = Split(str, " ")
For Each vStr In aStr
vStr = UCase(Left(vStr, 1)) & Right(vStr, Len(vStr) - 1)
myProperCase = myProperCase & vStr & " "
Next vStr
myProperCase = Trim(myProperCase)
End Function
 
you may want to add the following to handle an all cap input

JOHN SMITH
Code:
Public Function myProperCase(str As String) As String
  Dim aStr() As String
  Dim vStr As Variant
  If isAllCaps(str) Or isAllLower(str) Then
    myProperCase = StrConv(str, vbProperCase)
  Else
     str = Trim(str)
     aStr = Split(str, " ")
     For Each vStr In aStr
       vStr = UCase(Left(vStr, 1)) & Right(vStr, Len(vStr) - 1)
       myProperCase = myProperCase & vStr & " "
     Next vStr
     myProperCase = Trim(myProperCase)
  End If
End Function

Public Function isAllCaps(str As String) As Boolean
  isAllCaps = (StrComp(str, UCase(str), vbBinaryCompare) = 0)
End Function
Public Function isAllLower(str As String) As Boolean
  isAllLower = (StrComp(str, LCase(str), vbBinaryCompare) = 0)
End Function
 
Um.... StrConv() already handles this sort of basic ProperCase conversion. Re-read the original question, he wants something different.
 
Unfortunately, there are quite a few exceptions that are extremely difficult to accommodate.

Ex:

[tt]MacHenry or Machenry
Machinery or MacHinery[/tt]

and what about the single-quote/apostrophe issue.

[tt]O'Bryan or O'bryan
Can't or Can'T[/tt]

As soon as you get beyond the functionality provided by the StrConv function, data quickly becomes complicated and VERY troublesome to deal with.

-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
Hi Everyone,

This is what I came up with and it seems to work:

sBuildName = ""
sLastNameOld = "McMuffin"
sLastName = StrConv("McMuffin", vbProperCase)
' This will give you Mcmuffin

For i = 1 To Len(sLastNameOld)
If InStr("ABCDEFGHIJKLMNOPQRSTUVWXYZ", Mid(sLastNameOld, i, 1)) > 0 Then
sBuildName = sBuildName + Mid(sLastNameOld, i, 1)
Else
sBuildName = sBuildName + Mid(sLastName, i, 1)
End If
Next i
sLastName = sBuildName

It rebuilds the string one character at a time and puts a capital letter in the string if it is in the sLastNameOld string.


Mark

 
Er ... surely that only works if the entered name is already has correct capitalisation. Which is not what your original question asked.

If you feed it "ronald mcdonald" ...
 
Yea, that loop doesn't look like it's doing what you think it is...

One option is something like this
Code:
If LCase(Left(sLastNameOld, 2)) = "mc" Then
   sBuildName = "Mc" & UCase(Mid(sLastNameOld, 3, 1)) & LCase(Mid(sLastNameOld, 4))
End If

That should handle all the "mc"'s. Can be extended to handle other combinations like O'Leary and MacHenry, although you have situations where this falls apart (like George pointed out)... if the last name is Mack, you probably don't want to change it to MacK. John O'Leary Iii doesn't look so great either. And have fun fixing John's wife's name... mrs. mary dicarlo-o'leary :)
 
Um.... StrConv() already handles this sort of basic ProperCase conversion. Re-read the original question, he wants something different

Um... You need to reread the orginal post, not me! And no the strconv does not handle what the code does. The original post simply said
I have a name like "Ronald McDonald" and use the StrConv function I get "Ronald Mcdonald" instead of "Ronald McDonald" Is there a easy way to keep the capital letter in the last name?
My code will keep the capitilization in the middle and do a proper case conversion. The user said nothing about given "ronald mcdonald" figure out if there should be a capitalization in the middle and turn it into "Ronald McDonald
 
> The user said nothing about

Actually, they inferred that most strongly, almost explicitly. I'm sorry that you can't see that.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top