admin管理员组文章数量:1290177
I have a string with certain amount of mas. I want to find index of 5-th ma and then splice it. How can I do it?
Like this string: "This, is, my, javascript, string, to, present"
Turn into this: "This, is, my, javascript, string to, present"
I have a string with certain amount of mas. I want to find index of 5-th ma and then splice it. How can I do it?
Like this string: "This, is, my, javascript, string, to, present"
Turn into this: "This, is, my, javascript, string to, present"
8 Answers
Reset to default 3
var str = "This, is, my, javascript, string, to, present";
var i = 0, c = 5; // for flexibility c could be any number (3 for the third, ...)
while((i = str.indexOf(',', i + 1)) !== -1 && --c) // look for the fifth ma if any
;
if(i != -1) // if there is a fifth ma
str = str.substr(0, i) + str.substr(i + 1); // then remove it
console.log(str);
You could splice the array after slitting the string.
var string = 'This, is, my, javascript, string, to, present',
pos = 5,
temp = string.split(',');
temp.splice(pos -1, 0, temp.splice(pos - 1, 2).join(''));
console.log(temp.join(','));
1) The solution using String.prototype.replace() function:
var str = "This, is, my, javascript, string, to, present",
count = 0;
spliced = str.replace(/,/g, function(m){
return (++count == 5)? '' : m;
});
console.log(spliced);
2) The alternative solution using String.prototype.split()
and Array.prototype.slice()
functions:
var str = "This, is, my, javascript, string, to, present",
parts = str.split(','),
spliced = (parts.length > 5)? parts.slice(0, 5).join(',') + parts.slice(5).join(',') : parts.join(',');
console.log(spliced);
Try this;
function removeCharacterAtIndex(value, index) {
return value.substring(0, index) + value.substring(index + 1);
}
var input = "This, is, my, javascript, string, to, present";
console.log(removeCharacterAtIndex(input, 32));
var myStringArray = myString.split("");
var count = 0;
myStringArray.forEach(function(item, index){
if(item === ','){
count ++;
}
if (count ===5){
indexOf5thma = index;
}
});
myStringArray.splice(indexOf5thma, 1);
myString = myStringArray.join("");
Try something like this?
function getPosition(string, subString, index) {
return string.split(subString, index).join(subString).length;
}
Usage:
var myString = "This, is, my, javascript, string, to, present";
getPosition(myString, ',', 5);
Use some tricks on String.prototype.replace
:
function replace (str, word, pos) {
let cnt = 0
return str.replace(word, word => ++cnt == pos ? '' : word)
}
console.log(replace("This, is, my, javascript, string, to, present", ',', 5)
The second argument of String.prototype.replace
can be a function, which receives a matched string and returns the string to be place into the position. So we can use a scoped counter to determine which ma is to be removed.
Try like this:
var myString = "This, is, my, javascript, string, to, present";
var counter = 0;
myString = myString.split(""); // string to array
// find and replace 5th ma in array using counter
for (var i = 0; i < myString.length; i++) {
if (myString[i] === ",") {
counter++;
if (counter === 5) {
myString.splice(i, 1);
}
}
}
myString = myString.join(""); // array to string
本文标签: javascriptHow to remove nth character from the stringStack Overflow
版权声明:本文标题:javascript - How to remove nth character from the string? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741492524a2381688.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论