patternMinor
Capitalize string except for conjunction words
Viewed 0 times
wordsstringforcapitalizeconjunctionexcept
Problem
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
queryForHeading = xCompleteWord
queryForHeading = Mid(queryForHeading, 2)
queryForHeading = UCase(Mid(queryForHeading, 1, 1)) & Mid(queryForHeading, 2)Solution
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
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
specifically you should store your conjunction words in an ArrayList something like this
Dim conjunctions
Set conjunctions = CreateObject("System.Collections.Arraylist")
conjunctions.Add("a")
conjunctions.Add("an")
conjunctions.Add("and")
conjunctions.Add("as")
conjunctions.Add("at")
conjunctions.Add("by")
' 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
nextsuffixes 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")
conjunctions.Add(1,"a")
conjunctions.Add(2,"an")
conjunctions.Add(3,"and")
conjunctions.Add(4, "as")
conjunctions.Add(5, "at")
conjunctions.Add(6, "by")
' 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
nextsuffixes would be another dictionary. this could be used with the method that Vogel612 suggests.Code Snippets
Dim conjunctions
Set conjunctions = CreateObject("System.Collections.Arraylist")
conjunctions.Add("a")
conjunctions.Add("an")
conjunctions.Add("and")
conjunctions.Add("as")
conjunctions.Add("at")
conjunctions.Add("by")
' 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
nextDim conjunctions ' Create a variable.
Set conjunctions = CreateObject("Scripting.Dictionary")
conjunctions.Add(1,"a")
conjunctions.Add(2,"an")
conjunctions.Add(3,"and")
conjunctions.Add(4, "as")
conjunctions.Add(5, "at")
conjunctions.Add(6, "by")
' 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
nextContext
StackExchange Code Review Q#49859, answer score: 4
Revisions (0)
No revisions yet.