A.New
A.New

Reputation: 97

How to remove nth character from the string?

I have a string with certain amount of commas. I want to find index of 5-th comma 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"

Upvotes: 2

Views: 3712

Answers (8)

hsfzxjy
hsfzxjy

Reputation: 1322

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 comma is to be removed.

Upvotes: 1

RomanPerekhrest
RomanPerekhrest

Reputation: 92854

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

Upvotes: 2

Yurii Zadorozhnyi
Yurii Zadorozhnyi

Reputation: 36

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

Upvotes: 0

ibrahim mahrir
ibrahim mahrir

Reputation: 31692

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 comma if any
  ;

if(i != -1) // if there is a fifth comma
  str = str.substr(0, i) + str.substr(i + 1); // then remove it


console.log(str);

Upvotes: 3

user7528575
user7528575

Reputation:

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

Upvotes: 1

Nina Scholz
Nina Scholz

Reputation: 386600

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(','));

Upvotes: 2

Fanyo SILIADIN
Fanyo SILIADIN

Reputation: 792

var myStringArray = myString.split("");
var count = 0;
myStringArray.forEach(function(item, index){
 if(item === ','){
  count ++;
}
if (count ===5){
  indexOf5thcomma = index;
}
});
myStringArray.splice(indexOf5thcomma, 1);
myString = myStringArray.join("");

Upvotes: 1

Hasan
Hasan

Reputation: 2552

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

Upvotes: 1

Related Questions