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"

Share Improve this question edited Feb 7, 2017 at 12:17 RomanPerekhrest 92.9k4 gold badges74 silver badges112 bronze badges asked Feb 7, 2017 at 12:04 A.NewA.New 972 silver badges7 bronze badges
Add a ment  | 

8 Answers 8

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