查看“LlSubStringIndex”的源代码
←
LlSubStringIndex
跳转至:
导航
、
搜索
因为以下原因,您没有权限编辑本页:
您所请求的操作仅限于该用户组的用户使用:
用户
您可以查看与复制此页面的源代码。
{{LSL Header|ml=*}}{{LSLC|Keywords}}{{LSLC|Flow Control}}{{LSLC|}} {{函数详情 |函数名 = Function: integer llSubStringIndex( string source, string pattern ); |参数= 参数:•字符串源-要搜索的内容(haystack) •字符串源(string source)-要搜索的内容(haystack) •字符串模式(string pattern)-要搜索的内容(针) 如果在源代码中找不到模式,则返回-1。 字符串中第一个字符的索引为0 |返回值= 返回值:返回一个整数,该整数是源中模式的第一个实例的索引。 |注意事项=执行文字匹配(区分大小写)。 不支持通配符和正则表达式。 如果pattern是空字符串,则返回的值是0而不是-1。 没有从特定偏移量开始搜索字符串的函数。另请参阅,以获取从末尾搜索的函数。 |示例= 示例1 <pre> default { touch_start(integer num_detected) { string name = llDetectedName(0); integer spaceIndex = llSubStringIndex(name, " "); // no conditional check is needed for (spaceIndex == -1) // because we KNOW the legacy name must have a space // extract the substring from the first character to the one before the space string firstName = llGetSubString(name, 0, spaceIndex - 1); llSay(PUBLIC_CHANNEL, firstName + " touched me."); } } </pre> 示例2 <pre> default { state_entry() { llSensorRepeat("", NULL_KEY, AGENT_BY_LEGACY_NAME, PI, 96.0, 20); } sensor(integer num_detected) { // Loop through all the sensor data and match against " Linden", // this causes it to match with any last name of Linden (since there can't be spaces before the firstname) // Alternatively you could match a firstname with "FirstName " or anything else integer index; do { key avatarKey = llDetectedKey(index); string avatarLegacyName = llDetectedName(index); // watch out for the bitwise-NOT (~) // the next line translates to if (indexOfSearchedStringInOtherString != -1) integer isReallyALinden = ~llSubStringIndex(avatarLegacyName, " Linden"); if (isReallyALinden) llInstantMessage(avatarKey, "Hello, I see you!"); } while (++index < num_detected); } } </pre> 基本示例: <pre> integer index = llSubStringIndex("string data","TEST"); if(index == -1) { llSay(PUBLIC_CHANNEL,"TEST was not found in the string"); } else { llSay(PUBLIC_CHANNEL,"TEST was found in the string."); } </pre> 奶酪串(String Cheese) <pre> //This example shows how you can ask if a word or group of words is in a given string. //There is a limitation with this function. Your search of the string is for an exact match (case sensitive) //so the string_example below would be hard to match. string string_example = "ThIs serVes As aN exaMplE sTrinG. It ISn't toO coMPleX bUt HaS sOme mIlD vARietY"; //If you chat a question "Search for search_word" within range of the object this script is in //it will recognize (by searching the chat msg) the "search for" part and take the word or words following it //and check the string_example for those words. string search_test_a = "seArCh foR"; //The example below works the same way but searches for the word in front of the recognized trigger question. string search_test_b = "is the word I seek"; //Using this variable provides a way to manipulate the word(s) during the script without damaging the msg. string search_word; // Provide a mnemonic for the -1 return code that means NOT FOUND integer NOT_FOUND = -1; default { on_rez(integer param)//Although reseting the script on_rez provides many benefits { //in some cases it would be a bad idea because stored variables, lists and queued events would be trashed. llResetScript(); } state_entry() { //This is just for fun (but better to know what object is talking to you). llSetObjectName("String Cheese"); llListen(PUBLIC_CHANNEL, "", llGetOwner(), "");//Listen to you on the public chat channel for everything you say. } listen(integer chan, string name, key id, string msg) { if(llSubStringIndex(llToUpper(msg), llToUpper(search_test_a)) != NOT_FOUND) { search_word = llStringTrim(llGetSubString(msg, llStringLength(search_test_a), -1), STRING_TRIM); if(llSubStringIndex(llToUpper(string_example), llToUpper(search_word)) != NOT_FOUND) { llSay(PUBLIC_CHANNEL, "I have found the word " + "''" + search_word + "''" + " in the example string"); } else { llSay(PUBLIC_CHANNEL, "I cannot find the word " + "''" + search_word + "''" + " in the example string."); } } if(llSubStringIndex(msg, search_test_b) != NOT_FOUND) { search_word = llStringTrim(llGetSubString(msg, 0, (llSubStringIndex(msg, search_test_b)-1)), STRING_TRIM); if(llSubStringIndex(string_example, search_word) != NOT_FOUND) { llSay(PUBLIC_CHANNEL, "I have found the word " + "''" + search_word + "''" + " in the example string"); } else { llSay(PUBLIC_CHANNEL, "I cannot find the word " + "''" + search_word + "''" + " in the example string."); } } } } </pre> 有用的片段: <pre> 测试一个字符串是否包含另一个字符串的副本: 1简洁与传统: nteger contains(string haystack, string needle) // http://wiki.secondlife.com/wiki/llSubStringIndex { return 0 <= llSubStringIndex(haystack, needle); } integer startswith(string haystack, string needle) // http://wiki.secondlife.com/wiki/llSubStringIndex { return llDeleteSubString(haystack, llStringLength(needle), 0x7FFFFFF0) == needle; } integer endswith(string haystack, string needle) // http://wiki.secondlife.com/wiki/llSubStringIndex { return llDeleteSubString(haystack, 0x8000000F, ~llStringLength(needle)) == needle; } 注意:上面的一些片段返回结果时从未调用llSubStringIndex。 2聪明更小(计算包含在~54字节而不是~60字节): 注意:上面的一些片段返回结果时从未调用llSubStringIndex。 2聪明更小(计算包含在~54字节而不是~60字节): integer contains(string haystack, string needle) // http://wiki.secondlife.com/wiki/llSubStringIndex { return ~llSubStringIndex(haystack, needle); } 注意:llSubStringIndex函数只在找不到时返回-1,~运算符只为-1返回零,因此巧妙的组合~llSubStringIndex只为未找到返回零,否则为已找到返回非零。 注意:当我们的代码竞赛者和效率测试人员工具测量表达式{contains(“wiki.secondlife.com网站“,”wiki“);}。 </pre> |相关函数= [[llListFindList]] –在另一个列表中查找列表 [[llGetSubString]]–复制字符串的一部分 [[uSubStringLastIndex]] –返回一个整数,它是源中最后一个模式的索引。 |相关事件=无 }}
该页面使用的模板:
模板:LSL Header
(
查看源代码
)
模板:LSLC
(
查看源代码
)
模板:LSLGC
(
查看源代码
)
模板:Multi-lang
(
查看源代码
)
模板:函数详情
(
查看源代码
)
返回至
LlSubStringIndex
。
导航菜单
个人工具
登录
名字空间
页面
讨论
变种
视图
阅读
查看源代码
查看历史
更多
搜索
导航
网站首页
知识百科
编辑帮助
最近更改
工具
链入页面
相关更改
特殊页面
页面信息