Ivan
Ivan

Reputation: 525

Capitalize first letter of first word in a every sentence in ColdFusion

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!

Upvotes: 2

Views: 11663

Answers (5)

Frank Tudor
Frank Tudor

Reputation: 4534

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#">

Upvotes: 7

Andy
Andy

Reputation: 169

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".

Upvotes: 0

valueweaver
valueweaver

Reputation: 483

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

<cfset mystring = "lorem ipsum"/>

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

Upvotes: 1

Sanjeev
Sanjeev

Reputation: 1866

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");
}

Upvotes: 2

James A Mohler
James A Mohler

Reputation: 11120

I would use CSS rather than ColdFusion to do this

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

Upvotes: 5

Related Questions