[英]Actionscript 3 replacing string
I am trying to replace a substring in a string. 我正在尝试替换字符串中的子字符串。 My code below replaces all occurrences of substring.
我下面的代码替换了所有出现的子字符串。 Eg When clicked on in it replaces both in .
例如,当点击时,将替换为 。 But I like to replace only clicked one.
但是我只想替换一下即可。 How can we do this?
我们应该怎么做?
The books are in the table in my room. 这些书是在我的房间里的桌子。
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);
}
You can use TextField.getCharIndexAtPoint(x:Number, y:Number):int
to get the (0-based) index of the char in a particular coordinate. 您可以使用
TextField.getCharIndexAtPoint(x:Number, y:Number):int
获取特定坐标下char的(从0开始)索引。
You obviously need to use your clicked point as (x, y) . 显然,您需要将单击点用作(x,y) 。
You can use localX
and localY
from TextField.click
event. 您可以从
TextField.click
事件使用localX
和localY
。
See here for more: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/text/TextField.html#getCharIndexAtPoint%28%29 详情请参阅此处: http : //help.adobe.com/zh_CN/FlashPlatform/reference/actionscript/3/flash/text/TextField.html#getCharIndexAtPoint%28%29
At this point you need to search the string to replace in the neighborhood of the clicked char. 此时,您需要搜索要在单击的字符附近替换的字符串。
For example, something like this: 例如,如下所示:
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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.