Staredit Network

Staredit Network -> Computers and Technical -> [Javascript] Change a character
Report, edit, etc...Posted by Pie_Sniper on 2006-09-02 at 19:03:48
In Javascript, is there anyway to change a character at a specific/variable position in a string? Like

CODE
var String = "Bla bla bla"; String[2] = "y";

and String would become "Bly bla bla". I can get the character at a specific position, but I can't edit it for some reason.
Report, edit, etc...Posted by Doodle77(MM) on 2006-09-02 at 19:36:03
string.charAt(2) = "y"; ?
Report, edit, etc...Posted by Pie_Sniper on 2006-09-02 at 19:38:06
Already tried it; you can't do that.
Report, edit, etc...Posted by Vibrator on 2006-09-02 at 19:41:08
you could try using the slice function and another variable to create your own function
Report, edit, etc...Posted by Heimdal on 2006-09-02 at 19:42:13
javascript strings are immutable, so you cannot do that:

http://www.html.com/forums/javascript/6010...gs-mutable.html
Report, edit, etc...Posted by Pie_Sniper on 2006-09-02 at 21:17:35
CODE
function changeChar( STRING, ELEMENT, NEW )
{
 var String = STRING.slice( 0, ELEMENT );
 var String2 = STRING.slice( ELEMENT + 1, STRING.length );
 String += NEW;
 STRING = String.concat( String2 );
 return STRING;
}

var String = "This is a string.";
String = changeChar( String, 16, "!" ); // "This is a string!"

smile.gif Thanks, Vibrator.
Report, edit, etc...Posted by Pyro-Fire on 2006-09-20 at 03:49:13
or use php..
Report, edit, etc...Posted by Pie_Sniper on 2006-09-20 at 18:46:20
PHP would be worthless in my situation.
Next Page (1)