# Capitalize string except for conjunction words

I am using the code below to capitalize words in a string. I do not want conjunction words capitalized, but always want the first character in the string to be capitalized no matter what the word is. Is this the most optimized way of accomplishing this? How could this be improved?

xText = queryForHeading
xTextSplit = split(xText, " ")

for each item in xTextSplit
xWord = item
if lcase(item) = "a" or lcase(item) = "an" or lcase(item) = "and" or lcase(item) = "as" or lcase(item) = "at" or lcase(item) = "but" or lcase(item) = "by" or lcase(item) = "en" or lcase(item) = "for" or lcase(item) = "if" or lcase(item) = "in" or lcase(item) = "is" or lcase(item) = "nor" or lcase(item) = "of" or lcase(item) = "on" or lcase(item) = "or" or lcase(item) = "per" or lcase(item) = "the" or lcase(item) = "this" or lcase(item) = "to" or lcase(item) = "it" or lcase(item) = "vs." then
xWord = lcase(item)
elseif lcase(item) = "ii" or lcase(item) = "iii" or lcase(item) = "iv" or lcase(item) = "vi" or lcase(item) = "vii" or lcase(item) = "viii" or lcase(item) = "ix" then
xWord = ucase(item)
end if
xCompleteWord = xCompleteWord & " " & xWord
next


-

I think that you should be using ArrayLists for those conditional statements.

specifically you should store your conjunction words in an ArrayList something like this

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

' Etc ......

for each item in xTextSplit
xWord = item
if conjunctions.contains(lcase(item))
xWord = lcase(item)
elseif suffixes.contains(lcase(item))
xWord = ucase(item)
end if
xCompleteWord = xCompleteWord & " " & xWord
next


suffixes would be another ArrayList. this could be used with the method that Vogel612 suggests.

creating an ArrayList is a better way to perform this task because now you can add more words to the conjunction ArrayList and not have to worry if you found all the instances of it in the code. It is more Maintainable.

NOTE: I haven't tested this code

I think that you should be using Dictionary's for those conditional statements.

specifically you should store your conjunction words in a dictionary something like this

Dim conjunctions   ' Create a variable.
Set conjunctions = CreateObject("Scripting.Dictionary")

' Etc ......

for each item in xTextSplit
xWord = item
if conjunctions.Exist(lcase(item))
xWord = lcase(item)
elseif suffixes.Exist(lcase(item))
xWord = ucase(item)
end if
xCompleteWord = xCompleteWord & " " & xWord
next


suffixes would be another dictionary. this could be used with the method that Vogel612 suggests.

-

You could drastically shorten your If statements by extracting them to a function. Furthermore I nowhere see, that you actually change the case for xWord. That also makes the assignment at the start of your loop useless. Your code then changes to:

for each item in xTextSplit
if isConjunction(item) then
xWord = lcase(item)
elseif isRomanNumeral(item) then
xWord = ucase(item)
else
xWord = ucaseFirst(item)
end if
completeWord = completeWord & " " & xWord
next

private function isConjunction (String item) as Boolean