Page 1 of 1 1
Topic Options
#198471 - 2010-04-28 05:33 PM Capitalize First Letter Of Every Word In A String
stmatte Offline
Fresh Scripter

Registered: 2003-08-27
Posts: 7
Does anyone have a simple script or udf that will Capitalize First Letter Of Every Word In A String?

From;
capitalize first letter of every word in a string.

To;
Capitalize First Letter Of Every Word In A String.

Top
#198472 - 2010-04-28 05:40 PM Re: Capitalize First Letter Of Every Word In A String [Re: stmatte]
Allen Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4545
Loc: USA
First Option is fnProper - http://www.kixtart.org/forums/ubbthreads.php?ubb=showflat&Number=120570

Second is something I just put together with very limited testing...

 Code:
$string="this is a string"
? $string
? UCFirst($string)

function UCFirst($str)
  dim $arr,$i
  $arr=split($str," ")
  for $i= 0 to ubound($arr)
    $arr[$i]=ucase(left($arr[$i],1))+lcase(right($arr[$i],-1))
  next
  $UCFirst=join($arr," ")
endfunction

Top
#198473 - 2010-04-28 05:42 PM Re: Capitalize First Letter Of Every Word In A String [Re: stmatte]
Chris S. Offline
MM club member
*****

Registered: 2002-03-18
Posts: 2368
Loc: Earth
Give this a try...

fnProper()

Top
#198474 - 2010-04-28 05:44 PM Re: Capitalize First Letter Of Every Word In A String [Re: Chris S.]
Chris S. Offline
MM club member
*****

Registered: 2002-03-18
Posts: 2368
Loc: Earth
Damn you Allen! I get to answer 1-2 posts a year and you have to beat me to the punch. ;\)
Top
#198475 - 2010-04-28 05:49 PM Re: Capitalize First Letter Of Every Word In A String [Re: Chris S.]
Allen Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4545
Loc: USA
LOL. Okay I have to admit you just gave me some weird satisfaction. I remember back in the day when I was first started posting here, saying the same thing to someone else. \:\)

Hey... at least I remembered your UDF and gave you top billing

Top
#198480 - 2010-04-28 06:34 PM Re: Capitalize First Letter Of Every Word In A String [Re: Allen]
stmatte Offline
Fresh Scripter

Registered: 2003-08-27
Posts: 7
Thank you very much I will test it.
If you have time to answer a question I would appreciate it. I have only done simple programming in the past and just started to get in to stuff using arrays. I did the code below to try to solve my problem and it didn't work. If you have time could you tell me why?
$string = "hello there world"
$split = Split($string, " ")
For Each $i in $split
$i = UCase(Left($i, 1)) + Right($i, Len($i) - 1)
Next
$string = Join($split[$i], " ")

Thanks again.

Top
#198481 - 2010-04-28 06:45 PM Re: Capitalize First Letter Of Every Word In A String [Re: stmatte]
Allen Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4545
Loc: USA
The simple answer is... its wrong \:\) (although you were on the right path)

$i is one element/string of the $split array

Each time through the loop you are just over writing the string $i. Nothing is really changing.

If you look at my sample above, you will see I am overwriting the value in the array, and then once all the values are recreated, creating a string with the join.

Clear as mud?


Top
#198489 - 2010-04-28 08:43 PM Re: Capitalize First Letter Of Every Word In A String [Re: Allen]
stmatte Offline
Fresh Scripter

Registered: 2003-08-27
Posts: 7
Now I saw the difference, great thank you.
Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 259 anonymous users online.
Newest Members
gespanntleuchten, DaveatAdvanced, Paulo_Alves, UsTaaa, xxJJxx
17864 Registered Users

Generated in 0.057 seconds in which 0.023 seconds were spent on a total of 14 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org