12.4.搜索字串

问题
我想在字符串里找出指定的子串
解决办法
使用String类的indexOf( )或lastIndexOf( )方法
讨论
使用indexOf( )和lastIndexOf( )方法可检测出字符串中是否包含指定的子串,每个方法返回匹配子串的起始索引。indexOf( )方法从左到右搜索,而lastIndexOf( )方法从右到左搜索,如果没找到则返回-1。

indexOf( )方法接受两个参数:
substring
指定要搜索的子串
startIndex
可选参数,表示起始搜索位置,默认从0开始。

如果要测试是否一个字符串包含另一个字符串,只需要传入一个参数给indexOf( )方法即可:
+展开
-ActionScript
var example:String = "This string contains the word cool twice. Very cool.";
// 获得子串"cool"在example中的索引
var index:int = example.indexOf( "cool" );
// 如果indexOf( )方法返回-1,表示没有找到"cool"
if ( index != -1 ) {
// 显示:"String contains word cool at index 30"
TRace( "String contains word cool at index " + index );
}

通过指定第二个参数指定搜索的起始位置,再次搜索,找出第二个匹配的字串位置:
+展开
-ActionScript
var example:String = "This string contains the word cool twice. Very cool.";
// 得到第一个匹配的子串
var index:int = example.indexOf( "cool" );
if ( index != -1 ) {
// 显示: "String contains word cool at index 30"
trace( "String contains word cool at index " + index );
}
// 得到第二个匹配的子串"cool"
// 传递index + 1,以过滤掉第一个匹配子串,如果只传递index那么返回的仍是第一个子串位置
index = example.indexOf( "cool", index + 1 );
if ( index != -1 ) {
// 显示:"String contains word cool at index 47"
trace( "String contains word cool at index " + index );
}

在while循环语句中使用indexOf( )可找出所有匹配的子串位置,例如:
+展开
-ActionScript
var example:String = "This string contains the word cool. Very cool. Yes, cool.";
var index:int = -1;
// 循环直到indexOf()返回-1
while ( ( index = example.indexOf( "cool", index + 1 ) ) != -1 ) {
/* 显示:
String contains word cool at index 30
String contains word cool at index 41
String contains word cool at index 52
*/

trace( "String contains word cool at index " + index );
}


lastIndexOf( )方法与indexOf( )类似,只是它是从右边开始匹配搜索,返回第一个匹配的子串索引。

lastIndexOf( )方法也接受两个参数意义和indexOf( )相同:
如果搜不到返回-1,默认从字符串的末尾(字符串长度)开始搜索。
+展开
-ActionScript
var example:String = "This string contains the word cool twice. Very cool.";
//得到最后面的那个"cool"索引
var index:int = example.lastIndexOf( "cool" );
if ( index != -1 ) {
// 显示: "String contains word cool at index 47"
trace( "String contains word cool at index " + index );
}
// 得到第二个"cool"索引
// 通过传递index-1作为第二个参数
index = example.lastIndexOf( "cool", index - 1 );
if ( index != -1 ) {
// 显示: "String contains word cool at index 30" because the next to last
trace( "String contains word cool at index " + index );
}

和indexOf( )一样,在while循环里通过lastIndexOf( )找出所有匹配子串
+展开
-ActionScript
var example:String = "This string contains the word cool. Very cool. Yes, cool.";
var index:int = example.length;
// 循环直到lastIndexOf( )返回-1.
while ( ( index = example.lastIndexOf( "cool", index - 1 ) ) != -1 ) {
/* 显示:
String contains word cool at index 52
String contains word cool at index 41
String contains word cool at index 30
*/

trace( "String contains word cool at index " + index );
}

这里的代码和上面的indexOf( )很类似,只不过这里的index初始值为example.length而不是-1,因为lastIndexOf( )是从右边开始搜索,第二个不同点是lastIndexOf( )第二个参数不是index+1。

indexOf( )和lastIndexOf( )方法都是区分大小写的,比如搜索"cool"就不能匹配"Cool"因为大小写不同,要进行不区分大小写搜索,可使用toLowerCase( )方法进行转换。
+展开
-ActionScript
var example:String = "Cool. This is a string with the word cool. It spells"
" cool as both cool (lowercase) and Cool (capitalized).";
var search:String = "cool";
//输出第一个匹配的"cool",结果为37,
//因为第一个"Cool"是大写
trace( example.indexOf( search ) );
// 经过小写转换后再次搜索匹配,返回结果为0
trace( example.toLowerCase( ).indexOf( search ) );
// 输出最后一个匹配的"cool",结果为66,
trace( example.lastIndexOf( search ) );
// 经过小写转换后输出为87
trace( example.toLowerCase( ).lastIndexOf( search ) );
// 现在改变搜索子串为"Cool"
search = "Cool";
// 进行小写转换后输出结果为-1,因为已经没有大写"Cool"存在了
trace( example.toLowerCase( ).indexOf( search ) );

加支付宝好友偷能量挖...


评论(0)网络
阅读(111)喜欢(0)flash/flex/fcs/AIR