String ActionScript3替换字符串
我正在尝试替换字符串中的子字符串。下面的代码将替换所有出现的子字符串。例如,在中单击时,它将替换中的两个。但我只想换一个。我们怎样才能做到这一点 书在我房间的桌子里String ActionScript3替换字符串,string,actionscript-3,actionscript,String,Actionscript 3,Actionscript,我正在尝试替换字符串中的子字符串。下面的代码将替换所有出现的子字符串。例如,在中单击时,它将替换中的两个。但我只想换一个。我们怎样才能做到这一点 书在我房间的桌子里 function correct(e:TextEvent):void{ str =String(e.currentTarget.htmlText); if(e.text==replacements[e.currentTarget.name]){
function correct(e:TextEvent):void{
str =String(e.currentTarget.htmlText);
if(e.text==replacements[e.currentTarget.name]){
e.currentTarget.htmlText =strReplace(str, e.text, corrections[e.currentTarget.name]);
}
}
function strReplace(str:String, search:String, replace:String):String {
return str.split(search).join(replace);
}
您可以使用
TextField.getCharIndexAtPoint(x:Number,y:Number):int
获取特定坐标中字符的(基于0的)索引
显然,您需要将单击的点用作(x,y)
您可以使用TextField中的localX
和localY
。单击事件
请参见此处了解更多信息:
此时,您需要在单击的字符附近搜索要替换的字符串
例如,类似这样的内容:
stringToReplace = "in";
len = stringToReplace.Length;
neighborhood = TextField.substring(clickedCharIndex-len, clickedCharIndex+len);
if (neighborhood.indexOf(stringToReplace)) {
neighborhood = neighborhood.replace(stringToReplace, replacement);
TextField.text = TextField.text(0, clickedCharIndex-len) + neighborhood +
TextField.text(clickedCharIndex+len);
}