6
a=new String("Hello");

String("Hello")

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

Does this mean I can only use Strings as arrays of char's by .split("") and then .join("")?


ANSWER: Yes, in Javascript strings are readonly (aka immutable) This question is answered here at:

2
  • for all intensive purposes, for all I can see is that new String("hello") is virtually equivilant to just "hello". It has no extra array methods etc, and can be manipulated only through the same methods. w3schools.com/jsref/jsref_obj_string.asp Commented Sep 21, 2012 at 9:56
  • @PlacidCow it does have [] to get individual characters though Commented Sep 24, 2012 at 6:37

5 Answers 5

4

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

You could create a custom mutable String object, something like this experiment (esp. see method replaceCharAt).

Sign up to request clarification or add additional context in comments.

Comments

3

Thats correct.

You can of course build a function to handle this for you.

See this SO Post for different examples of this:

How do I replace a character at a particular index in JavaScript?

Comments

1

If it is necessary to be able to perform manipulations on your string as if it were an array or chars, that you perhaps create some prototypes:

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

returns hello, hbllo, hell, hellohbllohell, ehllo

1 Comment

Thanks for the detailed answer, but I was asking whether Javascript strings were readonly, all your answers are using a new string to replace the old one.
0

wouldn't the .valueOf() method of the Strings prototype return its primitive value ? Like,

enter image description here

Comments

0

Basically string in javascript are differentiated by two types one is primitive and another is object . String objects are character sequences .

You can use primitive type of string .

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.