F_WORDCAP

//Function Name : f_wordcap
//Argument Name : as_source, Arg Type : String, Pass By : Value
//Return Type :     String
//                        Returns string with the first letter of each word set to
//                        uppercase and the remaining letters lowercase if it succeeds
//                        and NULL if an error occurs.
//                        If any argument's value is NULL, function returns NULL.
//
//    Description:   Sets the first letter of each word in a string to a capital
//                        letter and all other letters to lowercase (for example,
//                        POWERBUILDER FUNCTION would be Powerbuilder Function).

integer    li_pos
boolean   lb_capnext
string      ls_ret
long        ll_stringlength
char        lc_char
char        lc_string[]

//Check parameters
If IsNull(as_source) Then
    string ls_null
    SetNull(ls_null)
    Return ls_null
End If

//Get and check length
ll_stringlength = Len(as_source)
If ll_stringlength = 0 Then
    Return as_source
End If

//Convert all characters to lowercase and put it into Character Array
lc_string = Lower(as_source)

//The first character should be capitalized
lb_capnext = TRUE

//Loop through the entire string
For li_pos = 1 to ll_stringlength
    //Get one character at a time
    lc_char = lc_string[li_pos]

    If Not f_IsAlpha(lc_char) Then
        //The next character should be capitalized
        lb_capnext = True
    ElseIf lb_capnext Then
        //Capitalize this Alphabetic character
        lc_string[li_pos] = Upper(lc_char)
        //The next character should not be capitalized
        lb_capnext = False
    End If
Next

//Copy the Character array back to a string variable
ls_ret = lc_string

//Return the
return ls_ret

5 comments:

shahmat said...

Good, but where is f_IsAlpha?

shahmat said...
This comment has been removed by the author.
shahmat said...
This comment has been removed by the author.
shahmat said...
This comment has been removed by the author.
shahmat said...

Anohter way...

//Recibe un string llamado cadena
//y retorna un string, cada espacio le indica
//que el próximo caracter será una mayúscula,
//si es otra cosa distinta a letra lo deja
//igual.
string ls_cadena, ls_caracter
long ll_longitud, ll_i
boolean lb_prox_letra_upper

string ls_cadena, ls_caracter
long ll_longitud, ll_i
boolean lb_prox_letra_upper

ll_longitud = Len( as_cadena )

if ll_longitud = 0 or isNull(ll_longitud) then
return as_cadena
end if

choose case ll_longitud
case 1
ls_cadena = Upper( as_cadena )
case is > 1
ls_cadena = Upper( Mid(as_cadena, 1, 1) )

for ll_i = 2 to ll_longitud
if lb_prox_letra_upper then
//leo caracter a caracter
ls_caracter = Upper(Mid(as_cadena, ll_i, 1))
else
//leo caracter a caracter
ls_caracter = Lower(Mid(as_cadena, ll_i, 1))
end if

ls_cadena += ls_caracter

if ls_caracter = ' ' then
lb_prox_letra_upper = true
else
lb_prox_letra_upper = false
end if

next
end choose

return ls_cadena

Post a Comment