I am using the code below to make a string have capitalization for the first letter in each word. I would like to take this a step further, and only capitalize all words that are not prepositions, or conjunctions (the, and, an, as, to) etc. Is this possible in classic ASP?
Convert this:
this is the best website on the web
To this:
This is the Best Website on the Web
I would think this is possible with RegEx, but I don't have a clue on where to start with that. Any help is appreciated.
queryForHTML = capCase(queryForHTML,true)
You could use a HashSet(Of String)
to store those exceptional words. Then split by white-space to get all words in a string, check whether you need to upper- or lowercase the first letter and use string.Join
to create a new string.
Here is a method that does it:
Private Shared ReadOnly CapCaseExceptions As New HashSet(Of String)(StringComparer.CurrentCultureIgnoreCase) From {
"the", "and", "an", "as", "to", "is", "on"
} ' etc.
Public Shared Function CapCase(input As String) As String
Dim words = From w In input.Split()
Let word = If(CapCaseExceptions.Contains(w),
Char.ToLower(w(0)) + w.Substring(1),
Char.ToUpper(w(0)) + w.Substring(1))
Select word
Return String.Join(" ", words)
End Function
Your sample input:
Dim input As String = "This Is The Best Webite On The Web"
Console.Write(CapCase(input)) ' This is the Best Webite on the Web
Edit: i'm not familiar with classic ASP, so i don't know if it helps.
There isn't anything built into classic ASP that is going to do this for you.
The only option I can think of is to build a dictionary of words that should not be capitalized and modify your code not to include those words.
This is my first idea. I'm sure you are able to improve that.
<%
xText = "This Is The Best Website On The Web"
xTextSplit = split(xText, " ")
for each item in xTextSplit
xWord = item
if lcase(item) = "the" or lcase(item) = "and" or lcase(item) = "an" or lcase(item) = "as" or lcase(item) = "to" or lcase(item) = "is" or lcase(item) = "on" then
xWord = lcase(item)
end if
xCompleteWord = xCompleteWord &" "& xWord
next
response.write xCompleteWord
%>
Output: This is the Best Website on the Web
Edit:
You can use CSS to capitalize the words as well (Note this will capitalize every word in lower case)
<div style="text-transform: capitalize;"><%=lcase(xCompleteWord)%></div>