Difference between revisions of "User:Peter Stindberg/strCapitalize"

From Second Life Wiki
Jump to navigation Jump to search
(Function to capitalize first letter in each word.)
 
Line 3: Line 3:
This function will take a string as input and return the string with the first letter of each word capitalized and all the other letters in lowercase. Works great for natural language, will fail with acronyms. For this, the "llToLower" could be removed.
This function will take a string as input and return the string with the first letter of each word capitalized and all the other letters in lowercase. Works great for natural language, will fail with acronyms. For this, the "llToLower" could be removed.


<source lang="lsl2">string strCapitalize(string str)
<syntaxhighlight lang="lsl">string strCapitalize(string str)
{
{
     list phrase = llParseStringKeepNulls(str, [" "], []);
     list phrase = llParseStringKeepNulls(str, [" "], []);
Line 11: Line 11:
     }
     }
     return llDumpList2String(phrase, " ");
     return llDumpList2String(phrase, " ");
}</source>
}</syntaxhighlight>

Revision as of 13:39, 8 July 2022

Capitalize first letter of each word

This function will take a string as input and return the string with the first letter of each word capitalized and all the other letters in lowercase. Works great for natural language, will fail with acronyms. For this, the "llToLower" could be removed.

string strCapitalize(string str)
{
    list phrase = llParseStringKeepNulls(str, [" "], []);
    integer i;
    for (i = 0; i < llGetListLength(phrase) ; i++) {
        phrase = llListReplaceList(phrase, [llToUpper(llGetSubString(llList2String(phrase,i),0,0)) + llToLower(llGetSubString(llList2String(phrase,i),1,-1))], i, i);
    }
    return llDumpList2String(phrase, " ");
}