Change A Character In A String Using Actionscript

前端 未结 4 1778
悲哀的现实
悲哀的现实 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 00:46
    //Replacing "/" with "-"
    var str:String = "he/ll/o"
    str = str.split("/").join("-"); //str = he-ll-o
    
    0 讨论(0)
  • 2021-01-19 00:51

    That answer was such a big help, but i think theres an easyer way. Suppose you want to replace the 4th character of a string called B by the letter "w". You can use

    B = B.replace(B.charAt(4), "w");
    

    Im using flash cs4 with actionscript 3.0, if it doesnt works with someone, let me know. If theres a evenbetter way of doing it let me know aswell.

    0 讨论(0)
  • 2021-01-19 00:56

    you cannot set any characters. Strings in ECMAScript (including ActionScript) are immutable. One thing you can do is to construct a new string containing the desired characters, as proposed here.

    However, if you plan to modify the string a lot, the best is to rather have an array of characters, that you can mutate at will. When you need to print it, you simply join it with "" as separator.

    greetz
    back2dos

    0 讨论(0)
  • 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);
    }
    
    0 讨论(0)
提交回复
热议问题