简体   繁体   中英

Remove characters from string and return new string with removed characters

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)) //lo 

I 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!

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 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)) //lo 

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)) //lo 

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, ie

'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) .

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)) //Ho 

只需让你的函数返回这个:string.substr(0,start)+ string.substr(charToRemove + 1)

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)) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM