2
votes

I want to get a string like this:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec tempor pulvinar enim! Nec aliquam massa faucibus sed?? Praesent nec consectetur sapien... Nulla dapibus rutrum turpis, ac porta erat posuere vel.

starting from string in all uppercase (or lowercase). For example:

LOREM IPSUM DOLOR SIT AMET, CONSECTETUR ADIPISCING ELIT. DONEC TEMPOR PULVINAR ENIM! NEC ALIQUAM MASSA FAUCIBUS SED?? PRAESENT NEC CONSECTETUR SAPIEN... NULLA DAPIBUS RUTRUM TURPIS, AC PORTA ERAT POSUERE VEL.

How can I do? Thank you!

5
What have you tried? Show us some code and specific errors/issues you are having.Scott Stroz
UpperFirst() might be helpful.Fish Below the Ice
I had an answer but did not read carfully - sorry :)Mark A Kruger

5 Answers

6
votes

Take your text and set it to a variable like this:

<cfset stringFixer = "LOREM IPSUM DOLOR SIT AMET, CONSECTETUR ADIPISCING ELIT. DONEC TEMPOR PULVINAR ENIM! NEC ALIQUAM MASSA FAUCIBUS SED?? PRAESENT NEC CONSECTETUR SAPIEN... NULLA DAPIBUS RUTRUM TURPIS, AC PORTA ERAT POSUERE VEL.">

Lowercase everything:

<cfset stringFixer = lCase(stringFixer)>

Then you will need to match your string terminator with rematch like this:

<cfset stringFixerBreaker = reMatch('\w.+?[.?]+',stringFixer)>

reMatch() will break apart your string into smaller discrete sentence strings...Then you could do a replaceNoCase() with left search for the first char then do the same with your replacement string which will be the same but we will throw a uCase() on that first character to capitalize it.

<cfloop array="#stringFixerBreaker#" index="i">
<cfoutput>#replaceNoCase(i,left(i, 1 ),uCase(left(i, 1 )))# </cfoutput>
</cfloop>  

Your output will look like this:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec tempor pulvinar enim! nec aliquam massa faucibus sed?? Praesent nec consectetur sapien... Nulla dapibus rutrum turpis, ac porta erat posuere vel.

Edit: One last touch point to my answer.

If you need to rebuild the string do this:

<cfset str = "">

<cfloop array="#stringFixerBreaker#" index="i">
    <cfset str = str & replaceNoCase(i,left(i, 1 ),uCase(left(i, 1 ))) & " ">
</cfloop>  

Dump out the results to check everything is in order:

<cfdump var="#str#">
6
votes

I would use CSS rather than ColdFusion to do this

<span style="text-transform : capitalize"><cfoutput>#lcase(mystring)#</cfoutput></span>
2
votes

While I personally like @James A Mohler solution, you can also do this in CF using a simple regex.

function Initcap(text){
    return rereplace(lcase(arguments.text), "(\b\w)", "\u\1", "all");
}
1
votes

The best way is by using regex and ReReplace or ReReplaceNoCase.

<cfset mystring = "lorem ipsum"/>

#ReReplace(mystring ,"\b(\w)","\u\1","ALL")#
0
votes

Here is my approach and it works in all cases except in a situation such as mc'donald's. I can get it to capitalize the "d" but then the "s" would also be capitalized. Mc'Donald'S.

 <cfif ISDefined('mywds')>
   <cfoutput>
      #mywds#<br>
       <cfset catz = #LCase(mywds)#>
          <cfloop index="dogz" list="#catz#" delimiters=" ">
             <cfif Len(dogz) is 1 >
                 #UCase(dogz)#
             <cfelse>
                 #Left(UCase(dogz), 1 )##Right(LCase(dogz),
                 Len(dogz) - 1 )#
             </cfif>
          </cfloop>
      </cfoutput>
  </cfif>


 <cfform action="" method="POST" target="_self">
     <input type="text" name="mywds" size="50"><br>
     <input type="submit" name="submit" value="Submit">
 </cfform>

This is a working form so you can paste in a .cfm page and it will function.

The 2nd cfif statement:

  <cfif Len(dogz) is 1 >

is so if someone enters text like "john t williams" the code will not throw an error on the single character "t".