0

我正在尝试替换字符串中的子字符串。我下面的代码替换了所有出现的子字符串。例如,当点击in时,它会同时替换in。但我喜欢只替换点击一个。我们应该怎么做?

我房间的桌子上。

          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);
        }
4

1 回答 1

0

您可以使用TextField.getCharIndexAtPoint(x:Number, y:Number):int来获取特定坐标中 char 的(基于 0 的)索引。

您显然需要将点击点用作(x, y)

您可以使用localXlocalYTextField.click事件。

请参阅此处了解更多信息:http: //help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/text/TextField.html#getCharIndexAtPoint%28%29

此时您需要在单击的字符附近搜索要替换的字符串。

例如,像这样:

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);

}
于 2013-01-21T10:06:19.453 回答