• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 168
  • Last Modified:

text formating

I am trying to find out the best way that once a person fills out a form that I may alter the case of the text inserted.

ex
if the person enters a name "bob" I would like to insert that into the db like "Bob.
and they insert a a long line of text "my company name is name"  then I would like to insert it as "My Company Name Is Name".

Any suggestions.
0
jriver12
Asked:
jriver12
  • 5
  • 4
1 Solution
 
MauseCommented:
Hi there,

So you want every first letter of a word in uppercase?
you can do this:

<CFSET text = "my company name is name">
<CFSET text_new = "">

<cfloop index="word" list="#text#" delimiters=" ">
      <CFSET text_new = text_new&' '&ucase(left(word,1))&right(word,len(word)-1)>
</cfloop>
<cfoutput>#text_new#</cfoutput>

Or even better try to find a rereplace function
(sorry no time to find out right now, maybe later)

hope this helps

Mause
0
 
jriver12Author Commented:
Mause,
your comment works if all letters are in lcase, but as we all know the user will sometimes put all in ucase, I would like to convert all strings to ucase(1) and the rest lower.
0
 
MauseCommented:
that it will be like this:
<CFSET text_new = text_new&' '&ucase(left(word,1))&lcase(right(word,len(word)-1))>
0
Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

 
jriver12Author Commented:
MAuse,
I am encountering an error when the string contains "&,-". EX a & B co?
0
 
MauseCommented:
what is the error?
0
 
jriver12Author Commented:
Error Diagnostic Information

An error occurred while evaluating the expression:


 company_new = company_new&' '&ucase(left(word,1))&lcase(right(word,len(word)-1))



Error near line 40, column 7.
--------------------------------------------------------------------------------

Parameter 2 of function Right which is now "0" must be a positive integer

COMPANYNAME=test & test
0
 
MauseCommented:
I see try this:

<cfloop index="word" list="#text#" delimiters=" ">
      <CFSET text_new = text_new&' #ucase(left(word,1))#'>
      <CFIF len(word) GT 1 >
      <CFSET text_new = text_new&lcase(right(word,len(word)-1))>
      </cfif>
</cfloop>

Also only when the first char of a word is a-z it will be A-Z
and there is a [SPACE] as first char of the new string so use TRIM when dispaly or insert/update db

Mause
0
 
jriver12Author Commented:
that worked great.
thank you much Sire!
0
 
MauseCommented:
NP
0
 
anandkpCommented:
I guess a better way wld have been to use functions available in backend - like INITCAP in Oracle :)
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now