Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

how to assign char in a string javascript

function replaceAt(str, index, newChar) {
    function replacer(origChar, strIndex) {
        if (strIndex === index)
            return newChar;
        else
            return origChar;
    }
    return str.replace(/./g, replacer);
}

let str = 'Hello World';
str = replaceAt(str, 1, 'u');
console.log(str); // Hullo World
 Run code snippet
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #assign #char #string #javascript
ADD COMMENT
Topic
Name
9+1 =