Answers for "how to assign char in a string javascript"

0

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
Posted by: Guest on October-17-2021

Code answers related to "how to assign char in a string javascript"

Code answers related to "Javascript"

Browse Popular Code Answers by Language