10 examples of 'replace first character in string javascript' in JavaScript

Every line of 'replace first character in string javascript' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
30function replaceAt(string, index, char) {
31 return string.substr(0, index) + char + string.substr(index + char.length);
32}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

148function replaceAt(value, index, character) {
149 return (
150 value.substr(0, index) +
151 character +
152 value.substr(index + character.length)
153 );
154}
160function stringReplacingCharacterAtIndexWithString(
161 str: string,
162 indexToReplace: number,
163 replacementString: string,
164): string {
165 return (
166 str.substr(0, indexToReplace) +
167 replacementString +
168 str.substr(indexToReplace + 1)
169 );
170}
14function replaceChar(match) {
15 return replacements[match];
16}
51function replaceAt(str, index, character) {
52 return str.substr(0, index) + character + str.substr(index+character.length);
53}
140function replaceLastCharacter(string, character) {
141 return string.slice(0, string.length - 1) + character;
142}
204function changePlus(string) {
205 for (var i = 0; i < string.length; i++) {
206 if (string.charAt(i) == " ") {
207 string = string.replaceAt(i, "+");
208 }
209 }
210 return string;
211}
10export function replaceSubstring(str, start, end, replacer) {
11 const prefix = str.substring(0, start);
12 let suffix = str.substring(end + 1);
13
14 // Ensure suffix starts with a SPACE,
15 // caret will be on the same line after replacing
16 if (suffix[0] !== ' ') {
17 suffix = ` ${suffix}`;
18 }
19
20 const prefixAndContent = `${prefix}${replacer}`;
21
22 return {
23 value: `${prefixAndContent}${suffix}`,
24 caret: prefixAndContent.length + 1,
25 };
26}
63function replace(char, pos, val) {
64 return one(char, val.charCodeAt(pos + 1), settings)
65}
62function replaceString(pString,pSearch,pReplace)
63// replaces in the string "pString" multiple substrings "pSearch" by "pReplace"
64{
65 //alert("cstring.js - replaceString() "+pString);
66 if (!pString) {
67 alert("replaceString()-Call - pString not defined!");
68 } else if (pString != '') {
69 {
70 //alert("cstring.js - replaceString() "+pString);
71 var vHelpString = '';
72 var vN = pString.indexOf(pSearch);
73 var vReturnString = '';
74 while (vN >= 0)
75 {
76 if (vN > 0)
77 vReturnString += pString.substring(0, vN);
78 vReturnString += pReplace;
79 if (vN + pSearch.length < pString.length) {
80 pString = pString.substring(vN+pSearch.length, pString.length);
81 } else {
82 pString = ''
83 }
84 vN = pString.indexOf(pSearch);
85 };
86 };
87 return vReturnString + pString;
88 }
89
90};

Related snippets