如何更改Linden Scripting Language(LSL)中字符串的一部分?

时间:2009-07-22 09:57:20

标签: string linden-scripting-language

我有一个字符串:

string cover = "withaname"

我想将withaname更改为withnoname

我希望将withnoname更改为withanamegoose

LSL中完成此操作的最佳方式是什么?

2 个答案:

答案 0 :(得分:2)

如果您有一个可以分隔数据的特殊字符,则可以使用llList2String等函数。

llGetSubString可能就是你真正想要的。我真的不明白你的确切问题。

string The_String = "withaname";
integer i = llSubStringIndex(The_String, "a");
The_String = llInsertString(llDeleteSubString(The_String, i, i), i, "no");
llSay(0, The_String);
// says "withnoname"

答案 1 :(得分:0)

再次,不确定你想要什么。 http://wiki.secondlife.com/wiki/Category:LSL_String#Useful_Functions

一些示例代码(抱歉,Google Prettify没有突出显示LSL):


default
{
    touch_start(integer num_detected)
    {
        string myString = "this is some text";

        //  if you simply want to change the value
        myString = "now diff"+"erent "+"text";
        llSay(PUBLIC_CHANNEL, myString);

        //  if you want to change the value only if substring in string
        if (llSubStringIndex(myString, "text") != -1)
        {
            myString = "string contained text as substring";
            llSay(PUBLIC_CHANNEL, myString);
        }

        //  if you want to replace a word in a sentence
        //  and you know the spelling and it's surrounded by spaces
        list words = llParseString2List(myString, [" "], []);
        integer found = llListFindList(words, ["text"]);
        if (found != -1)// or use: if (~found)
        {
            words = llListReplaceList(words, ["textReplacedWithAnotherWord"], found, found);
            myString = llDumpList2String(words, " ");
            llSay(PUBLIC_CHANNEL, myString);
        }
    }
}