Javascript string object readonly?

a=new String("Hello");

String ("Hello")

a[0]==="H" //true
a[0]="J"
a[0]==="J" //false
a[0]==="H" //true

Does this mean that I can only use strings as char arrays on .split(""), and then .join("")?


ANSWER: Yes, in Javascript strings are readonly(it is invariable) Answered to this question:

+5
source share
5 answers

The strings are immutable , so yes. ashould be reassigned if you want to change the line. You can also use slice: a = 'j'+a.slice(1)or replace: a = a.replace(/^h/i,'j').

String, - (. replaceCharAt).

+3

.

, , .

SO :

JavaScript?

+3

, , , :

String.prototype.splice = function(start,length,insert) {
    var a = this.slice(0, start);
    var b = this.slice(start+length, this.length);
    if (!insert) {insert = "";};
    return new String(a + insert + b);
};

String.prototype.push = function(insert) {
    var a = this
    return new String(a + insert);
};

String.prototype.pop = function() {
    return new String(this.slice(0,this.length-1));
};

String.prototype.concat = function() {
    if (arguments.length > 0) {
        var string = "";
        for (var i=0; i < arguments.length; i++) {
            string += arguments[i];
        };
        return new String(this + string);
    };
};

String.prototype.sort = function(funct) {
    var arr = [];
    var string = "";
    for (var i=0; i < this.length; i++) {
        arr.push(this[i]);
    };
    arr.sort(funct);
    for (var i=0; i < arr.length; i++) {
        string += arr[i];
    };
    return new String(string);
};

var a = new String("hello");
var b = a.splice(1,1,"b");
var c = a.pop();
var d = a.concat(b,c);
var e = a.sort();

, hbllo, , hellohbllohell, ehllo

+1

.valueOf() Strings ? ,

enter image description here

0

javascript : , - . .

You can use a primitive string type.

   var x = "hello";

   console.log(x);

     output : "hello"
    x = "j"+x.substring(1);
    console.log(x);
    output : "jello";

Or use

  var x = new String("hello");
  console.log(x.toString());
  x = "j"+x.substring(1);
0
source

All Articles