I have a string which contains a random character after every 5th character like:
let string = "You s$eem t%o be =very [happy? thes+e day$s!"
How can I remove the the unwanted characters after every 5 characters and get the correct string like this:
"You seem to be very happy these days!"
The characters to be removed may be any kind of characters like special characters, numbers and alphabets. The thing is they are inserted after every 5 characters in the string. So, the characters after every 5 characters need to be removed to get correct string.
I wrote this code to insert a character after every 5 characters but confused with removing it. So, please help me to do it.
var string = "You seem to be very happy these days!";
saltedString = string.split('').reduce((a, e, i)=> a + e + (i % 5 === 4 ? Math.random().toString(36).substr(2, 1) : ''), '');
console.log(saltedString)```
I have a string which contains a random character after every 5th character like:
let string = "You s$eem t%o be =very [happy? thes+e day$s!"
How can I remove the the unwanted characters after every 5 characters and get the correct string like this:
"You seem to be very happy these days!"
The characters to be removed may be any kind of characters like special characters, numbers and alphabets. The thing is they are inserted after every 5 characters in the string. So, the characters after every 5 characters need to be removed to get correct string.
I wrote this code to insert a character after every 5 characters but confused with removing it. So, please help me to do it.
var string = "You seem to be very happy these days!";
saltedString = string.split('').reduce((a, e, i)=> a + e + (i % 5 === 4 ? Math.random().toString(36).substr(2, 1) : ''), '');
console.log(saltedString)```
Share
Improve this question
edited Jun 28, 2021 at 6:49
Bibek Oli
asked Jun 28, 2021 at 6:18
Bibek OliBibek Oli
8861 gold badge10 silver badges27 bronze badges
7
- What code have you attempted? You should add that to your question. – Andy Commented Jun 28, 2021 at 6:19
- @Andy I added my attempt. Can you help me? – Bibek Oli Commented Jun 28, 2021 at 6:22
- 1 Those characters aren't spaced 5 characters apart btw. – Andy Commented Jun 28, 2021 at 6:25
- 1 No, they are 5 characters from each other, not every 5th character as you claim in your question. – Andy Commented Jun 28, 2021 at 6:31
- 1 @Andy The question says "after every 5th character" which I would interpret as every 6th character. I understand that one could interpret it differently, but with the example it should be clear anyway – A_A Commented Jun 28, 2021 at 6:35
5 Answers
Reset to default 6You can use regex similar to your example. It stores a group of 5 characters with (.{5})
and matches an additional (6th) character with .
. Then it replaces this with the group of 5 characters only, leaving out the 6th one.
const string = "You s$eem t%o be =very [happy? thes+e day$s!"
const saltedString = string.replace(/(.{5})./g,"$1")
console.log(saltedString)
In one line:
const s = '1234567890'.split('').filter((c, i) => (i + 1) % 5 !== 0).join('');
console.log(s);
// 12346789
const s = 'You s$eem t%o be =very [happy? thes+e day$s!'.split('').filter((c, i) => (i + 1) % 6 !== 0).join('');
console.log(s);
// You seem to be very happy these days!
Note that those characters are 6 apart, not 5.
You could split the string into an array, and then splice
out the 5th character making sure to decrement the variable that holds the length of the array by one.
const str = "You s$eem t%o be =very [happy? thes+e day$s!";
function removeChar(str, n) {
// Create an array from the string
const arr = str.split('');
// Iterate over the array
// We make sure to create a separate variable for the
// array length so that we can decrement when the
// condition is met
for (let i = 0, l = arr.length; i < l; i++) {
// We want to match the index of the character to `n`
// We use modulo here to check the remainder,
// but we don't want to eliminate the character at index 0
if (i > 0 && i % 5 === 0) {
// When the condition is met, splice out the element
// and decrement the length value
arr.splice(i, 1);
l--;
}
}
// Return a joined-up array
return arr.join('');
}
console.log(removeChar(str, 5));
Split and join the string with that condition.
let string = "You s$eem t%o be =very [happy? thes+e day$s!";
let startIndex = 0;
let newStrArray = [];
while (startIndex < string.length) {
newStrArray.push(string.substring(startIndex, startIndex + 5));
startIndex += 6;
}
console.log(newStrArray.join(''));
Created a string.removeAt(index)
method removes character at specified index. A little solution but very effective !
let string = "You s$eem t%o be =very [happy? thes+e day$s!"
String.prototype.removeAt = function (index) {
let array = Array.from(this);
delete array[index];
let str = array.reduce((acc, char)=> acc += char, "")
return str
}
// Function loops through string and remove character after every specified index
function clear(string , index) {
for (let i = index; i < string.length; i = i + index) {
string = string.removeAt(i)
}
return string;
}
console.log(clear(string, 5))
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744239809a4564650.html
评论列表(0条)