Remove characters from string and return new string with removed characters
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I'm trying to remove a few characters from a string and return the removed characters. Here's my code...
function removeFromString(string, start, charToRemove){
var newString = ''
newString = string.slice(start, charToRemove);
return newString
}
alert(removeFromString ('Hello', 0, 3)) //loI figured out how to return the remaining characters but I can't figure out how to return the characters that were removed.
Thanks in advance!
javascript
add a comment |
I'm trying to remove a few characters from a string and return the removed characters. Here's my code...
function removeFromString(string, start, charToRemove){
var newString = ''
newString = string.slice(start, charToRemove);
return newString
}
alert(removeFromString ('Hello', 0, 3)) //loI figured out how to return the remaining characters but I can't figure out how to return the characters that were removed.
Thanks in advance!
javascript
you never remove characters from a string, because strings are immutable. you could select the other characters and return them.
– Nina Scholz
Nov 24 '18 at 16:57
My apologies, I'm not trying to mutate the string, I'm trying to put the characters in a new string and return that.
– Marsden Mars
Nov 24 '18 at 16:58
Right now it returns ('Hel'), I'm trying to get it to return ('lo')
– Marsden Mars
Nov 24 '18 at 16:59
add a comment |
I'm trying to remove a few characters from a string and return the removed characters. Here's my code...
function removeFromString(string, start, charToRemove){
var newString = ''
newString = string.slice(start, charToRemove);
return newString
}
alert(removeFromString ('Hello', 0, 3)) //loI figured out how to return the remaining characters but I can't figure out how to return the characters that were removed.
Thanks in advance!
javascript
I'm trying to remove a few characters from a string and return the removed characters. Here's my code...
function removeFromString(string, start, charToRemove){
var newString = ''
newString = string.slice(start, charToRemove);
return newString
}
alert(removeFromString ('Hello', 0, 3)) //loI figured out how to return the remaining characters but I can't figure out how to return the characters that were removed.
Thanks in advance!
function removeFromString(string, start, charToRemove){
var newString = ''
newString = string.slice(start, charToRemove);
return newString
}
alert(removeFromString ('Hello', 0, 3)) //lofunction removeFromString(string, start, charToRemove){
var newString = ''
newString = string.slice(start, charToRemove);
return newString
}
alert(removeFromString ('Hello', 0, 3)) //lojavascript
javascript
asked Nov 24 '18 at 16:56
Marsden MarsMarsden Mars
254
254
you never remove characters from a string, because strings are immutable. you could select the other characters and return them.
– Nina Scholz
Nov 24 '18 at 16:57
My apologies, I'm not trying to mutate the string, I'm trying to put the characters in a new string and return that.
– Marsden Mars
Nov 24 '18 at 16:58
Right now it returns ('Hel'), I'm trying to get it to return ('lo')
– Marsden Mars
Nov 24 '18 at 16:59
add a comment |
you never remove characters from a string, because strings are immutable. you could select the other characters and return them.
– Nina Scholz
Nov 24 '18 at 16:57
My apologies, I'm not trying to mutate the string, I'm trying to put the characters in a new string and return that.
– Marsden Mars
Nov 24 '18 at 16:58
Right now it returns ('Hel'), I'm trying to get it to return ('lo')
– Marsden Mars
Nov 24 '18 at 16:59
you never remove characters from a string, because strings are immutable. you could select the other characters and return them.
– Nina Scholz
Nov 24 '18 at 16:57
you never remove characters from a string, because strings are immutable. you could select the other characters and return them.
– Nina Scholz
Nov 24 '18 at 16:57
My apologies, I'm not trying to mutate the string, I'm trying to put the characters in a new string and return that.
– Marsden Mars
Nov 24 '18 at 16:58
My apologies, I'm not trying to mutate the string, I'm trying to put the characters in a new string and return that.
– Marsden Mars
Nov 24 '18 at 16:58
Right now it returns ('Hel'), I'm trying to get it to return ('lo')
– Marsden Mars
Nov 24 '18 at 16:59
Right now it returns ('Hel'), I'm trying to get it to return ('lo')
– Marsden Mars
Nov 24 '18 at 16:59
add a comment |
7 Answers
7
active
oldest
votes
I modified your own code to return the remaining characters:
function removeFromString(string, start, charToRemove){
var newString = '';
newString = string.slice(0, start) + string.slice(start+charToRemove);
return newString;
}
console.log(removeFromString ('Hello', 0, 3)) //loadd a comment |
You can convert the string to an array, use splice method to split the unused items, then joining the result items in the array to a string.
var arr = Array.from('Hello');
var items = arr.splice(3, arr.length);
var result = items.join('');
console.log(result);
This solution is not complete. It assumes the removal always starts at 0.
– Alimo
Nov 25 '18 at 13:25
add a comment |
You could map the characters and remove the caracters who are in the given range.
function removeFromString(string, start, charToRemove) {
return Array.from(
string,
(c, i) => i >= start && i < start + charToRemove ? '': c
).join('');
}
console.log(removeFromString ('Hello', 0, 3)) //loadd a comment |
string.slice(start, charToRemove) will return a new string value, which is created by taking all the characters from the string value inside the variable string from index start to index charToRemove
With your example call now, the string value inside the string variable is:
H e l l o
0 1 2 3 4
and the start index you give is 0 and charToRemove is 3, so we get a new string value back that contains the characters at index 0, 1 and 2 which is H, e and l.
If you want everything starting from index 3 you need to give 3 as a offset to the slice method, i.e.
'Hello'.slice(3)
I'm not entirely sure what exactly your goal is, though. If you want to get the last n characters in a string you can give a negative offset like string.slice(-2).
add a comment |
You could use substring to get the part that you want to remove and replace it with empty string.
This way we don't need loop, or converting the string to array.
function removeFromString(str, start, charToRemove){
let end = start+charToRemove
let strToBeRemoved = str.substring(start, end)
return str.replace(strToBeRemoved, "")
}
console.log(removeFromString ('Hello', 0, 3)) //lo
console.log(removeFromString ('Hello', 2, 2)) //Heo
console.log(removeFromString ('Hello', 1, 3)) //Hoadd a comment |
Just make your function return this : string.substr(0,start)+string.substr(charToRemove+1)
add a comment |
Here's a "multislice" function that cuts the string at specific positions and returns an array of slices. For example, segment(hello, 0, 3) returns [hel, lo].
function segment(str, ...positions) {
let last = 0, res =
for (let pos of positions) {
res.push(str.slice(last, pos))
last = pos
}
if (last < str.length)
res.push(str.slice(last))
return res
}
console.log(segment('Hello', 0, 3))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%2f53460413%2fremove-characters-from-string-and-return-new-string-with-removed-characters%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
7 Answers
7
active
oldest
votes
7 Answers
7
active
oldest
votes
active
oldest
votes
active
oldest
votes
I modified your own code to return the remaining characters:
function removeFromString(string, start, charToRemove){
var newString = '';
newString = string.slice(0, start) + string.slice(start+charToRemove);
return newString;
}
console.log(removeFromString ('Hello', 0, 3)) //loadd a comment |
I modified your own code to return the remaining characters:
function removeFromString(string, start, charToRemove){
var newString = '';
newString = string.slice(0, start) + string.slice(start+charToRemove);
return newString;
}
console.log(removeFromString ('Hello', 0, 3)) //loadd a comment |
I modified your own code to return the remaining characters:
function removeFromString(string, start, charToRemove){
var newString = '';
newString = string.slice(0, start) + string.slice(start+charToRemove);
return newString;
}
console.log(removeFromString ('Hello', 0, 3)) //loI modified your own code to return the remaining characters:
function removeFromString(string, start, charToRemove){
var newString = '';
newString = string.slice(0, start) + string.slice(start+charToRemove);
return newString;
}
console.log(removeFromString ('Hello', 0, 3)) //lofunction removeFromString(string, start, charToRemove){
var newString = '';
newString = string.slice(0, start) + string.slice(start+charToRemove);
return newString;
}
console.log(removeFromString ('Hello', 0, 3)) //lofunction removeFromString(string, start, charToRemove){
var newString = '';
newString = string.slice(0, start) + string.slice(start+charToRemove);
return newString;
}
console.log(removeFromString ('Hello', 0, 3)) //loanswered Nov 24 '18 at 17:07
AlimoAlimo
914
914
add a comment |
add a comment |
You can convert the string to an array, use splice method to split the unused items, then joining the result items in the array to a string.
var arr = Array.from('Hello');
var items = arr.splice(3, arr.length);
var result = items.join('');
console.log(result);
This solution is not complete. It assumes the removal always starts at 0.
– Alimo
Nov 25 '18 at 13:25
add a comment |
You can convert the string to an array, use splice method to split the unused items, then joining the result items in the array to a string.
var arr = Array.from('Hello');
var items = arr.splice(3, arr.length);
var result = items.join('');
console.log(result);
This solution is not complete. It assumes the removal always starts at 0.
– Alimo
Nov 25 '18 at 13:25
add a comment |
You can convert the string to an array, use splice method to split the unused items, then joining the result items in the array to a string.
var arr = Array.from('Hello');
var items = arr.splice(3, arr.length);
var result = items.join('');
console.log(result);You can convert the string to an array, use splice method to split the unused items, then joining the result items in the array to a string.
var arr = Array.from('Hello');
var items = arr.splice(3, arr.length);
var result = items.join('');
console.log(result);var arr = Array.from('Hello');
var items = arr.splice(3, arr.length);
var result = items.join('');
console.log(result);var arr = Array.from('Hello');
var items = arr.splice(3, arr.length);
var result = items.join('');
console.log(result);answered Nov 24 '18 at 17:01
FooFoo
1
1
This solution is not complete. It assumes the removal always starts at 0.
– Alimo
Nov 25 '18 at 13:25
add a comment |
This solution is not complete. It assumes the removal always starts at 0.
– Alimo
Nov 25 '18 at 13:25
This solution is not complete. It assumes the removal always starts at 0.
– Alimo
Nov 25 '18 at 13:25
This solution is not complete. It assumes the removal always starts at 0.
– Alimo
Nov 25 '18 at 13:25
add a comment |
You could map the characters and remove the caracters who are in the given range.
function removeFromString(string, start, charToRemove) {
return Array.from(
string,
(c, i) => i >= start && i < start + charToRemove ? '': c
).join('');
}
console.log(removeFromString ('Hello', 0, 3)) //loadd a comment |
You could map the characters and remove the caracters who are in the given range.
function removeFromString(string, start, charToRemove) {
return Array.from(
string,
(c, i) => i >= start && i < start + charToRemove ? '': c
).join('');
}
console.log(removeFromString ('Hello', 0, 3)) //loadd a comment |
You could map the characters and remove the caracters who are in the given range.
function removeFromString(string, start, charToRemove) {
return Array.from(
string,
(c, i) => i >= start && i < start + charToRemove ? '': c
).join('');
}
console.log(removeFromString ('Hello', 0, 3)) //loYou could map the characters and remove the caracters who are in the given range.
function removeFromString(string, start, charToRemove) {
return Array.from(
string,
(c, i) => i >= start && i < start + charToRemove ? '': c
).join('');
}
console.log(removeFromString ('Hello', 0, 3)) //lofunction removeFromString(string, start, charToRemove) {
return Array.from(
string,
(c, i) => i >= start && i < start + charToRemove ? '': c
).join('');
}
console.log(removeFromString ('Hello', 0, 3)) //lofunction removeFromString(string, start, charToRemove) {
return Array.from(
string,
(c, i) => i >= start && i < start + charToRemove ? '': c
).join('');
}
console.log(removeFromString ('Hello', 0, 3)) //loanswered Nov 24 '18 at 17:02
Nina ScholzNina Scholz
199k15112182
199k15112182
add a comment |
add a comment |
string.slice(start, charToRemove) will return a new string value, which is created by taking all the characters from the string value inside the variable string from index start to index charToRemove
With your example call now, the string value inside the string variable is:
H e l l o
0 1 2 3 4
and the start index you give is 0 and charToRemove is 3, so we get a new string value back that contains the characters at index 0, 1 and 2 which is H, e and l.
If you want everything starting from index 3 you need to give 3 as a offset to the slice method, i.e.
'Hello'.slice(3)
I'm not entirely sure what exactly your goal is, though. If you want to get the last n characters in a string you can give a negative offset like string.slice(-2).
add a comment |
string.slice(start, charToRemove) will return a new string value, which is created by taking all the characters from the string value inside the variable string from index start to index charToRemove
With your example call now, the string value inside the string variable is:
H e l l o
0 1 2 3 4
and the start index you give is 0 and charToRemove is 3, so we get a new string value back that contains the characters at index 0, 1 and 2 which is H, e and l.
If you want everything starting from index 3 you need to give 3 as a offset to the slice method, i.e.
'Hello'.slice(3)
I'm not entirely sure what exactly your goal is, though. If you want to get the last n characters in a string you can give a negative offset like string.slice(-2).
add a comment |
string.slice(start, charToRemove) will return a new string value, which is created by taking all the characters from the string value inside the variable string from index start to index charToRemove
With your example call now, the string value inside the string variable is:
H e l l o
0 1 2 3 4
and the start index you give is 0 and charToRemove is 3, so we get a new string value back that contains the characters at index 0, 1 and 2 which is H, e and l.
If you want everything starting from index 3 you need to give 3 as a offset to the slice method, i.e.
'Hello'.slice(3)
I'm not entirely sure what exactly your goal is, though. If you want to get the last n characters in a string you can give a negative offset like string.slice(-2).
string.slice(start, charToRemove) will return a new string value, which is created by taking all the characters from the string value inside the variable string from index start to index charToRemove
With your example call now, the string value inside the string variable is:
H e l l o
0 1 2 3 4
and the start index you give is 0 and charToRemove is 3, so we get a new string value back that contains the characters at index 0, 1 and 2 which is H, e and l.
If you want everything starting from index 3 you need to give 3 as a offset to the slice method, i.e.
'Hello'.slice(3)
I'm not entirely sure what exactly your goal is, though. If you want to get the last n characters in a string you can give a negative offset like string.slice(-2).
answered Nov 24 '18 at 17:09
StefanStefan
60028
60028
add a comment |
add a comment |
You could use substring to get the part that you want to remove and replace it with empty string.
This way we don't need loop, or converting the string to array.
function removeFromString(str, start, charToRemove){
let end = start+charToRemove
let strToBeRemoved = str.substring(start, end)
return str.replace(strToBeRemoved, "")
}
console.log(removeFromString ('Hello', 0, 3)) //lo
console.log(removeFromString ('Hello', 2, 2)) //Heo
console.log(removeFromString ('Hello', 1, 3)) //Hoadd a comment |
You could use substring to get the part that you want to remove and replace it with empty string.
This way we don't need loop, or converting the string to array.
function removeFromString(str, start, charToRemove){
let end = start+charToRemove
let strToBeRemoved = str.substring(start, end)
return str.replace(strToBeRemoved, "")
}
console.log(removeFromString ('Hello', 0, 3)) //lo
console.log(removeFromString ('Hello', 2, 2)) //Heo
console.log(removeFromString ('Hello', 1, 3)) //Hoadd a comment |
You could use substring to get the part that you want to remove and replace it with empty string.
This way we don't need loop, or converting the string to array.
function removeFromString(str, start, charToRemove){
let end = start+charToRemove
let strToBeRemoved = str.substring(start, end)
return str.replace(strToBeRemoved, "")
}
console.log(removeFromString ('Hello', 0, 3)) //lo
console.log(removeFromString ('Hello', 2, 2)) //Heo
console.log(removeFromString ('Hello', 1, 3)) //HoYou could use substring to get the part that you want to remove and replace it with empty string.
This way we don't need loop, or converting the string to array.
function removeFromString(str, start, charToRemove){
let end = start+charToRemove
let strToBeRemoved = str.substring(start, end)
return str.replace(strToBeRemoved, "")
}
console.log(removeFromString ('Hello', 0, 3)) //lo
console.log(removeFromString ('Hello', 2, 2)) //Heo
console.log(removeFromString ('Hello', 1, 3)) //Hofunction removeFromString(str, start, charToRemove){
let end = start+charToRemove
let strToBeRemoved = str.substring(start, end)
return str.replace(strToBeRemoved, "")
}
console.log(removeFromString ('Hello', 0, 3)) //lo
console.log(removeFromString ('Hello', 2, 2)) //Heo
console.log(removeFromString ('Hello', 1, 3)) //Hofunction removeFromString(str, start, charToRemove){
let end = start+charToRemove
let strToBeRemoved = str.substring(start, end)
return str.replace(strToBeRemoved, "")
}
console.log(removeFromString ('Hello', 0, 3)) //lo
console.log(removeFromString ('Hello', 2, 2)) //Heo
console.log(removeFromString ('Hello', 1, 3)) //Hoedited Nov 24 '18 at 17:30
answered Nov 24 '18 at 17:10
Christhofer NataliusChristhofer Natalius
855923
855923
add a comment |
add a comment |
Just make your function return this : string.substr(0,start)+string.substr(charToRemove+1)
add a comment |
Just make your function return this : string.substr(0,start)+string.substr(charToRemove+1)
add a comment |
Just make your function return this : string.substr(0,start)+string.substr(charToRemove+1)
Just make your function return this : string.substr(0,start)+string.substr(charToRemove+1)
answered Nov 24 '18 at 17:15
KingkostiaKingkostia
1
1
add a comment |
add a comment |
Here's a "multislice" function that cuts the string at specific positions and returns an array of slices. For example, segment(hello, 0, 3) returns [hel, lo].
function segment(str, ...positions) {
let last = 0, res =
for (let pos of positions) {
res.push(str.slice(last, pos))
last = pos
}
if (last < str.length)
res.push(str.slice(last))
return res
}
console.log(segment('Hello', 0, 3))add a comment |
Here's a "multislice" function that cuts the string at specific positions and returns an array of slices. For example, segment(hello, 0, 3) returns [hel, lo].
function segment(str, ...positions) {
let last = 0, res =
for (let pos of positions) {
res.push(str.slice(last, pos))
last = pos
}
if (last < str.length)
res.push(str.slice(last))
return res
}
console.log(segment('Hello', 0, 3))add a comment |
Here's a "multislice" function that cuts the string at specific positions and returns an array of slices. For example, segment(hello, 0, 3) returns [hel, lo].
function segment(str, ...positions) {
let last = 0, res =
for (let pos of positions) {
res.push(str.slice(last, pos))
last = pos
}
if (last < str.length)
res.push(str.slice(last))
return res
}
console.log(segment('Hello', 0, 3))Here's a "multislice" function that cuts the string at specific positions and returns an array of slices. For example, segment(hello, 0, 3) returns [hel, lo].
function segment(str, ...positions) {
let last = 0, res =
for (let pos of positions) {
res.push(str.slice(last, pos))
last = pos
}
if (last < str.length)
res.push(str.slice(last))
return res
}
console.log(segment('Hello', 0, 3))function segment(str, ...positions) {
let last = 0, res =
for (let pos of positions) {
res.push(str.slice(last, pos))
last = pos
}
if (last < str.length)
res.push(str.slice(last))
return res
}
console.log(segment('Hello', 0, 3))function segment(str, ...positions) {
let last = 0, res =
for (let pos of positions) {
res.push(str.slice(last, pos))
last = pos
}
if (last < str.length)
res.push(str.slice(last))
return res
}
console.log(segment('Hello', 0, 3))answered Nov 24 '18 at 17:34
georggeorg
155k35209307
155k35209307
add a comment |
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%2f53460413%2fremove-characters-from-string-and-return-new-string-with-removed-characters%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
you never remove characters from a string, because strings are immutable. you could select the other characters and return them.
– Nina Scholz
Nov 24 '18 at 16:57
My apologies, I'm not trying to mutate the string, I'm trying to put the characters in a new string and return that.
– Marsden Mars
Nov 24 '18 at 16:58
Right now it returns ('Hel'), I'm trying to get it to return ('lo')
– Marsden Mars
Nov 24 '18 at 16:59