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.)
 
m
 
(4 intermediate revisions by the same user not shown)
Line 1: Line 1:
=== Capitalize first letter of each word ===
{{LSLC|User-Defined_Functions}}
 
{{LSL_Function
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.
|func=strCapitalize
 
|func_desc=Created by [[User:Peter Stindberg|Peter Stindberg]], 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. <syntaxhighlight lang="lsl">string strCapitalize(string str)
<source lang="lsl2">string strCapitalize(string str)
{
{
     list phrase = llParseStringKeepNulls(str, [" "], []);
     list phrase = llParseStringKeepNulls(str, [" "], []);
Line 11: Line 10:
     }
     }
     return llDumpList2String(phrase, " ");
     return llDumpList2String(phrase, " ");
}</source>
}</syntaxhighlight>
|func_footnote
|return_type=string
|return_text=with each word capitalized.
|p1_type=string
|p1_name=str
|p1_desc=Word or phrase that will be capitalized.
|p1_hover
|constants
|spec
|caveats=Will fail with acronyms since all letters but the first will be turned to lowercase.
|examples
|helpers
|also_header
|also_functions
|also_tests
|also_events
|also_articles
|also_footer
|notes
|mode
|deprecated
|location
|cat1
|cat2
|cat3
|cat4
|cat5
|cat6
}}

Latest revision as of 22:04, 8 July 2022

Summary

Function: string strCapitalize( string str );

Created by Peter Stindberg, 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.
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, " ");
}

Returns a string with each word capitalized.
• string str Word or phrase that will be capitalized.

Caveats

Will fail with acronyms since all letters but the first will be turned to lowercase.

All Issues ~ Search JIRA for related Bugs

Examples

Deep Notes

Search JIRA for related Issues

Signature

function string strCapitalize( string str );