Replace part of string at specified position
I want to replace a part of string at specified position(start, end) by another string in javascript
Here is an example:
"Hello world this is a question"
I want to replace the part of this string starting at 5 and ending at 10 by "friends"
The output will be:
"Hello friends this is a question"
javascript
add a comment |
I want to replace a part of string at specified position(start, end) by another string in javascript
Here is an example:
"Hello world this is a question"
I want to replace the part of this string starting at 5 and ending at 10 by "friends"
The output will be:
"Hello friends this is a question"
javascript
add a comment |
I want to replace a part of string at specified position(start, end) by another string in javascript
Here is an example:
"Hello world this is a question"
I want to replace the part of this string starting at 5 and ending at 10 by "friends"
The output will be:
"Hello friends this is a question"
javascript
I want to replace a part of string at specified position(start, end) by another string in javascript
Here is an example:
"Hello world this is a question"
I want to replace the part of this string starting at 5 and ending at 10 by "friends"
The output will be:
"Hello friends this is a question"
javascript
javascript
asked Nov 25 '18 at 15:18
Amadou BeyeAmadou Beye
942515
942515
add a comment |
add a comment |
5 Answers
5
active
oldest
votes
For example with substring()
calls and concatenation (+
):
var msg="Hello world this is a question";
var replaced=msg.substring(0,6)+"friends"+msg.substring(11);
console.log(replaced);
accepted for the snippet ! Works great ! Thanks
– Amadou Beye
Nov 25 '18 at 15:31
add a comment |
Method 1:
If you know the exact index you want to slice the string at, you should probably use javascript string.slice method like so:
var str = "Hello world!";
var part = str.slice(1, 5);
console.log(part); // "ello"
Method 2:
If you don't know the index, but you do know the string you want to replace, you can simply use string.replace method like so:
var input = "Hello world this is a question";
var result = input.replace("world", "friends");
console.log(result); // Hello friends this is a question
add a comment |
You can use slice
to achieve this.
let str = "Hello world this is a question"
function replace(st, en, val) {
str = str.slice(0, st + 1) + val + str.slice(en + 1)
}
replace(5, 10, 'friends')
console.log(str)
add a comment |
You can try the replace()
and substring()
method
var str = "Hello world this is a question";
console.log(str.replace(str.substring(6, 11), "friends"));
add a comment |
there is replace function in javascript
var replaceData = "Hello world this is a question";
console.log(replaceData.replace('world', 'friends'));
the word to replace can appears multiple times and I only want to replace one
– Amadou Beye
Nov 25 '18 at 15:34
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53468915%2freplace-part-of-string-at-specified-position%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
For example with substring()
calls and concatenation (+
):
var msg="Hello world this is a question";
var replaced=msg.substring(0,6)+"friends"+msg.substring(11);
console.log(replaced);
accepted for the snippet ! Works great ! Thanks
– Amadou Beye
Nov 25 '18 at 15:31
add a comment |
For example with substring()
calls and concatenation (+
):
var msg="Hello world this is a question";
var replaced=msg.substring(0,6)+"friends"+msg.substring(11);
console.log(replaced);
accepted for the snippet ! Works great ! Thanks
– Amadou Beye
Nov 25 '18 at 15:31
add a comment |
For example with substring()
calls and concatenation (+
):
var msg="Hello world this is a question";
var replaced=msg.substring(0,6)+"friends"+msg.substring(11);
console.log(replaced);
For example with substring()
calls and concatenation (+
):
var msg="Hello world this is a question";
var replaced=msg.substring(0,6)+"friends"+msg.substring(11);
console.log(replaced);
var msg="Hello world this is a question";
var replaced=msg.substring(0,6)+"friends"+msg.substring(11);
console.log(replaced);
var msg="Hello world this is a question";
var replaced=msg.substring(0,6)+"friends"+msg.substring(11);
console.log(replaced);
answered Nov 25 '18 at 15:23
tevemadartevemadar
4,3382723
4,3382723
accepted for the snippet ! Works great ! Thanks
– Amadou Beye
Nov 25 '18 at 15:31
add a comment |
accepted for the snippet ! Works great ! Thanks
– Amadou Beye
Nov 25 '18 at 15:31
accepted for the snippet ! Works great ! Thanks
– Amadou Beye
Nov 25 '18 at 15:31
accepted for the snippet ! Works great ! Thanks
– Amadou Beye
Nov 25 '18 at 15:31
add a comment |
Method 1:
If you know the exact index you want to slice the string at, you should probably use javascript string.slice method like so:
var str = "Hello world!";
var part = str.slice(1, 5);
console.log(part); // "ello"
Method 2:
If you don't know the index, but you do know the string you want to replace, you can simply use string.replace method like so:
var input = "Hello world this is a question";
var result = input.replace("world", "friends");
console.log(result); // Hello friends this is a question
add a comment |
Method 1:
If you know the exact index you want to slice the string at, you should probably use javascript string.slice method like so:
var str = "Hello world!";
var part = str.slice(1, 5);
console.log(part); // "ello"
Method 2:
If you don't know the index, but you do know the string you want to replace, you can simply use string.replace method like so:
var input = "Hello world this is a question";
var result = input.replace("world", "friends");
console.log(result); // Hello friends this is a question
add a comment |
Method 1:
If you know the exact index you want to slice the string at, you should probably use javascript string.slice method like so:
var str = "Hello world!";
var part = str.slice(1, 5);
console.log(part); // "ello"
Method 2:
If you don't know the index, but you do know the string you want to replace, you can simply use string.replace method like so:
var input = "Hello world this is a question";
var result = input.replace("world", "friends");
console.log(result); // Hello friends this is a question
Method 1:
If you know the exact index you want to slice the string at, you should probably use javascript string.slice method like so:
var str = "Hello world!";
var part = str.slice(1, 5);
console.log(part); // "ello"
Method 2:
If you don't know the index, but you do know the string you want to replace, you can simply use string.replace method like so:
var input = "Hello world this is a question";
var result = input.replace("world", "friends");
console.log(result); // Hello friends this is a question
edited Nov 25 '18 at 15:29
answered Nov 25 '18 at 15:23
Ronen CypisRonen Cypis
14.7k11221
14.7k11221
add a comment |
add a comment |
You can use slice
to achieve this.
let str = "Hello world this is a question"
function replace(st, en, val) {
str = str.slice(0, st + 1) + val + str.slice(en + 1)
}
replace(5, 10, 'friends')
console.log(str)
add a comment |
You can use slice
to achieve this.
let str = "Hello world this is a question"
function replace(st, en, val) {
str = str.slice(0, st + 1) + val + str.slice(en + 1)
}
replace(5, 10, 'friends')
console.log(str)
add a comment |
You can use slice
to achieve this.
let str = "Hello world this is a question"
function replace(st, en, val) {
str = str.slice(0, st + 1) + val + str.slice(en + 1)
}
replace(5, 10, 'friends')
console.log(str)
You can use slice
to achieve this.
let str = "Hello world this is a question"
function replace(st, en, val) {
str = str.slice(0, st + 1) + val + str.slice(en + 1)
}
replace(5, 10, 'friends')
console.log(str)
let str = "Hello world this is a question"
function replace(st, en, val) {
str = str.slice(0, st + 1) + val + str.slice(en + 1)
}
replace(5, 10, 'friends')
console.log(str)
let str = "Hello world this is a question"
function replace(st, en, val) {
str = str.slice(0, st + 1) + val + str.slice(en + 1)
}
replace(5, 10, 'friends')
console.log(str)
answered Nov 25 '18 at 15:29
Nitish NarangNitish Narang
2,9401815
2,9401815
add a comment |
add a comment |
You can try the replace()
and substring()
method
var str = "Hello world this is a question";
console.log(str.replace(str.substring(6, 11), "friends"));
add a comment |
You can try the replace()
and substring()
method
var str = "Hello world this is a question";
console.log(str.replace(str.substring(6, 11), "friends"));
add a comment |
You can try the replace()
and substring()
method
var str = "Hello world this is a question";
console.log(str.replace(str.substring(6, 11), "friends"));
You can try the replace()
and substring()
method
var str = "Hello world this is a question";
console.log(str.replace(str.substring(6, 11), "friends"));
var str = "Hello world this is a question";
console.log(str.replace(str.substring(6, 11), "friends"));
var str = "Hello world this is a question";
console.log(str.replace(str.substring(6, 11), "friends"));
edited Nov 25 '18 at 15:34
Prashant Pimpale
3,3273830
3,3273830
answered Nov 25 '18 at 15:24
Doug FDoug F
196210
196210
add a comment |
add a comment |
there is replace function in javascript
var replaceData = "Hello world this is a question";
console.log(replaceData.replace('world', 'friends'));
the word to replace can appears multiple times and I only want to replace one
– Amadou Beye
Nov 25 '18 at 15:34
add a comment |
there is replace function in javascript
var replaceData = "Hello world this is a question";
console.log(replaceData.replace('world', 'friends'));
the word to replace can appears multiple times and I only want to replace one
– Amadou Beye
Nov 25 '18 at 15:34
add a comment |
there is replace function in javascript
var replaceData = "Hello world this is a question";
console.log(replaceData.replace('world', 'friends'));
there is replace function in javascript
var replaceData = "Hello world this is a question";
console.log(replaceData.replace('world', 'friends'));
var replaceData = "Hello world this is a question";
console.log(replaceData.replace('world', 'friends'));
var replaceData = "Hello world this is a question";
console.log(replaceData.replace('world', 'friends'));
answered Nov 25 '18 at 15:25
Krishna JonnalagaddaKrishna Jonnalagadda
1,2341719
1,2341719
the word to replace can appears multiple times and I only want to replace one
– Amadou Beye
Nov 25 '18 at 15:34
add a comment |
the word to replace can appears multiple times and I only want to replace one
– Amadou Beye
Nov 25 '18 at 15:34
the word to replace can appears multiple times and I only want to replace one
– Amadou Beye
Nov 25 '18 at 15:34
the word to replace can appears multiple times and I only want to replace one
– Amadou Beye
Nov 25 '18 at 15:34
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53468915%2freplace-part-of-string-at-specified-position%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown