stmatte
(Fresh Scripter)
2010-04-28 05:33 PM
Capitalize First Letter Of Every Word In A String

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.


AllenAdministrator
(KiX Supporter)
2010-04-28 05:40 PM
Re: Capitalize First Letter Of Every Word In A String

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


Chris S.
(MM club member)
2010-04-28 05:42 PM
Re: Capitalize First Letter Of Every Word In A String

Give this a try...

fnProper()


Chris S.
(MM club member)
2010-04-28 05:44 PM
Re: Capitalize First Letter Of Every Word In A String

Damn you Allen! I get to answer 1-2 posts a year and you have to beat me to the punch. ;\)

AllenAdministrator
(KiX Supporter)
2010-04-28 05:49 PM
Re: Capitalize First Letter Of Every Word In A String

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


stmatte
(Fresh Scripter)
2010-04-28 06:34 PM
Re: Capitalize First Letter Of Every Word In A String

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.


AllenAdministrator
(KiX Supporter)
2010-04-28 06:45 PM
Re: Capitalize First Letter Of Every Word In A String

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?



stmatte
(Fresh Scripter)
2010-04-28 08:43 PM
Re: Capitalize First Letter Of Every Word In A String

Now I saw the difference, great thank you.