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:

Community
  • 1
  • 1
TiansHUo
  • 8,509
  • 7
  • 45
  • 57
  • 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. http://www.w3schools.com/jsref/jsref_obj_string.asp – C.. Sep 21 '12 at 09:56
  • @PlacidCow it does have [] to get individual characters though – TiansHUo Sep 24 '12 at 06:37

5 Answers5

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

KooiInc
  • 119,216
  • 31
  • 141
  • 177
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?

Community
  • 1
  • 1
Curtis
  • 101,612
  • 66
  • 270
  • 352
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

C..
  • 802
  • 4
  • 16
  • 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. – TiansHUo Sep 24 '12 at 06:31
0

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

enter image description here

Moritz Roessler
  • 8,542
  • 26
  • 51
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);
kannanrbk
  • 6,964
  • 13
  • 53
  • 94