replaceAt() in javascript ,

By TSN ...

The Sample shows a Functions to repalce a character in the string .

The Below Function shows how we can write a extendeed function to a string in javascript.

We have many function for string something like charAt(), indexOf(). but we don,t have function that is used to replace a character in a string.

The below example shows that how we can write  extensoin functions in javascript and also use that function.

String.prototype.replaceAt = function (i, char) {
            return this.substr(0, i) + char + this.substr(i + char.length);
         }


Now we access the above function like the below shown

document.getElementById('txtname').value .replaceAt(2, "0")

txtame is the Id of the TextBox  and .value returns a string . To that string we are appliying a extended function replaceAt();

replaceAt() in javascript ,  (2494 Views)