KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Method for capitalizing the first letter of each word
PRODUCT: 4D | VERSION: 12.1 | PLATFORM: Mac & Win
Published On: March 17, 2011

The following method can be used to capitalize the first letter of each word in a string. The definition of a different word in this example is a character preceeded by a space, carriage return, or line feed. For example the string:

"hi my name is tom
i like to eat apples"


Would be converted to:

"Hi My Name Is Tom
I Like To Eat Apples"


The sample method is shown below:

  //Method: UTIL_capitalizeFirst
  //Inputs: $1 -> String -> String to capitalize
  //Output: $0 -> String -> Capitalized string
  //Description:
  //The method capitalizes the first letter of every word


C_TEXT($to_upper; $1; $0)
C_LONGINT($i)
$to_upper:=$1
$to_upper[[1]]:=Uppercase($to_upper[[1]])
For ($i;0;(Length($to_upper)-1))
  If ((Position(" ";$to_upper;$i;*)=$i) | \
      (Position(Char(Carriage return);$to_upper;$i;*)=$i) | \
      (Position(Char(Line feed);$to_upper;$i;*)=$i))
    $to_upper[[$i+1]]:=Uppercase($to_upper[[$i+1]])
  End if
End for
$0:=$to_upper


A sample call is given below:

$capitalized:=UTIL_capitalizeFirst($mystring)