分析一个通用的搜索和替换字符串函数在Flash 5中

在本教程中,我们将分析一个通用的搜索和替换字符串函数在Flash 5中的ActionScript编写的。
我相信,字符串操作,可视化对于很多人来说是一个困难的概念。这也难怪,因为即使是最简单的字符串处理似乎过于复杂和神秘的。学完本教程,这是我的希望,你将有征服的字符串操作的基本概念。
例如电影
请随时与下面的示例电影的实验。输入不同的输入,搜索和替换字符串,看看有什么输出时。
电影组件
我不会去太多的电影是如何设置的详细信息,但这里的基本知识:
1帧
1按钮
3输入文本框:string_in,string_to_search,string_to_replace
1动态TextField:string_out
该功能
以下是我们将要分析的全部功能。不要担心,如果它看起来有点吓人,我们将通过一步一步的。我只是想这可能是很好看整个事情。在这个例子中的电影,此函数位于第1帧中。
函数searchandreplace(the_string,SEARCH_STRING,replace_string,事件,向后){
如果“(SEARCH_STRING == replace_string)
返回the_string;
}
VAR = 0;
(向后== TRUE){
VAR(pos = the_string.lastIndexOf上SEARCH_STRING);
而(正> = 0){
发现+ +;
(0 start_string = the_string.substr,POS);
VAR end_string = the_string.substr(POS + search_string.length);
the_string = start_string + replace_string + end_string;
POS = the_string.lastIndexOf(SEARCH_STRING,start_string.length);
如果(发现==出现的){
POS = -1;
}
}
}
其他{
VAR(pos = the_string.indexOf上SEARCH_STRING);
而(正> = 0){
发现+ +;
(0 start_string = the_string.substr,POS);
VAR end_string = the_string.substr(POS + search_string.length);
the_string = start_string + replace_string + end_string;
POS = the_string.indexOf(SEARCH_STRING,POS + replace_string.length);
如果(发现==出现的){
POS = -1;
}
}
}
返回the_string;
}
(责任编辑:卓想动画http://www.zxgzs.com)