4
\$\begingroup\$

I am using the code below to capitalize a string properly. I would like to know if there is a better way to code the surname portion, as the way it stands now can become very bloated depending on how many surnames I plan on adding.

This is a follow up to: Capitalize string except for conjunction words

Dim conjunctions
Dim suffixes
Dim surnames
Set conjunctions = CreateObject("System.Collections.Arraylist")
Set suffixes = CreateObject("System.Collections.Arraylist")
Set surnames = CreateObject("System.Collections.Arraylist")

conjunctions.Add("a")
conjunctions.Add("an")
conjunctions.Add("and")
conjunctions.Add("as")
conjunctions.Add("at")
' etc....

suffixes.Add("ii")
suffixes.Add("iii")
suffixes.Add("iv")
suffixes.Add("vi")
suffixes.Add("vii")
suffixes.Add("viii")
suffixes.Add("ix")

surnames.Add("mcdonald")

for each item in xTextSplit
    xWord = item
    if conjunctions.contains(lcase(item)) then
        xWord = lcase(item)
    elseif suffixes.contains(lcase(item)) then
        xWord = ucase(item)
    elseif surnames.contains(lcase(item)) then
        xWord = "Mc" & UCase(Mid(item, 3, 1)) & Mid(item, 4)
    end if
    xCompleteWord = xCompleteWord & " " & xWord
next

queryForHeading = xCompleteWord
queryForHeading = Mid(queryForHeading, 2)
queryForHeading = UCase(Mid(queryForHeading, 1, 1)) & Mid(queryForHeading, 2)
\$\endgroup\$
4
  • \$\begingroup\$ this is a really good question. what if you have something like O'neill or other prefixes as well? \$\endgroup\$
    – Malachi
    Commented May 15, 2014 at 21:33
  • 1
    \$\begingroup\$ this really depends on where you are getting the information and what you are going to be doing with the information. I would probably try writing the code out and seeing what you can come up with that will work for a majority of test cases. \$\endgroup\$
    – Malachi
    Commented May 15, 2014 at 21:36
  • 1
    \$\begingroup\$ How are you going to deal with a surname of "van den Platz"? "de Wolfe"? "vanCamp"? \$\endgroup\$ Commented May 15, 2014 at 22:32
  • \$\begingroup\$ Yes, there are a lot of situations to account for. I can only hope I code for most of them! \$\endgroup\$ Commented May 15, 2014 at 23:24

1 Answer 1

3
\$\begingroup\$

If you are going to create a list of the last names it will get very big, but you would have a list of the names so you could instead

surnames.Add("McDonald")

And then

else
    foreach name in surnames
        if lcase(item) = lcase(name) then
            xWord = name
        end if
    next
end if

But, this would get rather long as you add the names in, which is a big downfall when talking about maintenance especially on a VBScript.

\$\endgroup\$
2
  • \$\begingroup\$ This is definitely something to build on. How come when I try something like O'Neil, it won't keep the capitalization on the N? \$\endgroup\$ Commented May 15, 2014 at 23:11
  • \$\begingroup\$ if you have it stored in your ArrayList like O'Neil it will work, but if you have it stored O'neil than that is what will be returned \$\endgroup\$
    – Malachi
    Commented May 15, 2014 at 23:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.