更改字符在字符串中使用ActionScript字符串、字符、ActionScript

2023-09-08 12:49:05 作者:细雨流光

什么是相反的 String.charAt() ??

如果我有一个字符串:

VAR海峡:字符串=Hello World的;

如何更改的第五字符,例如,从一个'到'_'?

How do I change the 5th character, for example, from a ' ' to an '_'?

我可以得到5个字符是这样的:

I can GET the 5th character like this:

变种C:字符串= Str.charAt(5);

但如何设置的第5个字符?

But how do I SET the 5th character?

在此先感谢。

推荐答案

有很多方法对皮肤这只猫。一,从我的头顶,将涉及使用String.substr:

There are many ways to skin this cat. One, off the top of my head, would involve String.substr:

var Str:String="Hello World"
var newStr:String = Str.substr(0,5) + "_" + Str.substr(6);

或,与上述相同,但更广义:

or, the same as above, but more generalized:

function setCharAt(str:String, char:String,index:int):String {
    return str.substr(0,index) + char + str.substr(index + 1);
}
 
精彩推荐
图片推荐