Change A Character In A String Using Actionscript

前端 未结 4 1781
悲哀的现实
悲哀的现实 2021-01-19 00:09

What is the opposite of String.charAt()??

If I Have a string:

var Str:String=\"Hello World\";

How do I change the 5th chara

4条回答
  •  北荒
    北荒 (楼主)
    2021-01-19 01:04

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

提交回复
热议问题