功能:将指定字符串中某子串替换成另一个字符串,并返回新字符串。用处极广。
用法:
replace (原字符串,要替换的子串,用来替换的子串)
原字符串为 “Hi, a river is a story “,使用replace (”Hi, a river is a story “,”a”,”their”) ,则返回字符串“Hi, their river is their story”。常常用来替换词,屏蔽不好的用语等。
function replace (str:String, replace:String, replaceWith:String):String
{
var sb:String = new String();
var found:Boolean = false;
for (var i = 0; i < str.length; i++)
{
if(str.charAt(i) == replace.charAt(0))
{
found = true;
for(var j = 0; j < replace.length; j++)
{
if(!(str.charAt(i + j) == replace.charAt(j)))
{
found = false;
break;
}
}
if(found)
{
sb += replaceWith;
i = i + (replace.length - 1);
continue;
}
}
sb += str.charAt(i);
}
return sb;
}