LlStringTrim

来自人工智能助力教育知识百科
跳转至: 导航搜索

Template:Needs Translation/


函数名
Function: string llStringTrim( string src, integer type );
参数:

•字符串src(string src)

•整数类型(integer type)–STRING_TRIM*标志

返回值:从一个空格中返回一个空格。
注意事项
示例
示例1
无论是通过聊天还是通过记事本卡,无论是通过聊天还是通过记事本接受用户的非结构化输入时,最好始终对其进行全面调整:
llStringTrim("User input", STRING_TRIM);

此示例返回已删除的字符串中前导和尾随“空白”字符的数目(不是特别有用,但演示了如何使用函数)。

default
{
    state_entry()
    {
        llListen(4, "", llGetOwner(), "");
    }
 
    on_rez(integer start_param)
    {
        llResetScript();
    }
 
    listen(integer channel, string name, key id, string message)
    {       
        //track the length
        integer length = llStringLength(message);
 
        //trim message (not necessary to store these to variables but makes reading easier)
        string trim_left  = llStringTrim(message, STRING_TRIM_HEAD);
        string trim_right = llStringTrim(message, STRING_TRIM_TAIL);
        string trim_both  = llStringTrim(message, STRING_TRIM);
 
        //output the results
        llOwnerSay("Initial length  = " + (string)length +
                 "\nLeading Spaces  = " + (string)(length - llStringLength(trim_left))+
                 "\nTrailing Spaces = " + (string)(length - llStringLength(trim_right))+
                 "\nTrimmed Message = \"" + trim_both + "\"");
    }
}
除了开头和/或结尾处的空白,实际的字符串将不受影响。不过,这也意味着字符串中多余的空格(例如,错误的双空格类型)将不会被更正。
下面将删除所有双空格、前导空格和尾随空格。
llDumpList2String(llParseString2List(src, [" "], []), " "); //works but can use a large quantity of memory
这种方法从句子中去掉空格,并且可能使用较少的内存
//Added By To-mos Codewarrior
string str1 = "some words to remove the spaces from";
integer index;
while(~index=llSubStringIndex(str1," ")) {
    data=llDeleteSubString(str1,index,index);
}
以下操作也将执行相同的操作:
(string)llParseString2List(src, [" "], []); //works but can use a large quantity of memory
llList2Json还修剪列表中包含的字符串。因此,修剪整个字符串列表的简单方法是:
list l = ["  a  ", " b", "c ", " d "];
list trimmedList = llJson2List(llList2Json(JSON_ARRAY, l));
相关函数
相关事件