|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Adjusting text to title case
I think I'm overthinking this. I need to take a person's name or a sentence and convert it from the case it is to title case.
So, "the dog was smart" would become "The Dog Was Smart" ColdFusion has ucase and lcase, neither will work on it's own to do what I want. I've thought about running a list loop on the statement with the delimiter being the space. As each word comes in, the first letter will be capitalized. The code is: <cfloop index="curWord" list="#passedString#" delimiters=" "> <cfset firstLetter = left(curWord,'1')> <cfset wordLen = len(curWord)> <cfset finishedWord = right(curWord,wordLen - 1)> <cfset finalValue = finalValue & ucase(firstLetter) & lcase(finishedWord) & " "> </cfloop> This would work well for names, but I'm afraid it will drag on long sentences. Am I way overthinking this? Is there a better way to do it? |
|
#2
|
|||
|
|||
|
Always check cflib.org first. http://www.cflib.org/udf.cfm?ID=116
__________________
Ask if you have a question, but also help answer questions that you have knowledge of! Thanks, Brian. How to Post a Question in the Forums |
|
#3
|
|||
|
|||
|
The UDF that Kiteless links to is very good, and your method is pretty fine, too. A small speeding-up would be:
Code:
<cfset Finished = "">
<cfloop list="My_Sentence" delimiters=" " index="C1">
<cfset Finished = Finished
& " "
& Ucase(Left(C1, 1))
& Iif(Len(C1 GT 1), DE(Right(C1, Len(C1) - 1), DE(""))>
</cfloop>
<cfset Finished = trim(Finished)>
|
|
#4
|
|||
|
|||
|
Thanks, guys!
|
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > ColdFusion Development > Adjusting text to title case |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|