-3

I have a string like this:

var str = "this is a ttest";  // t   h   i   s     i   s     a      t   t   e   s   t
                              // 1   2   3   4  5  6   7  8  9  10  11  12  13  14  15

Now I need to replace eighth character (t) with nothing. How can I do that?

Something like this: .replace("{eighth character}", "");

Finally I want this output:

var newstr = "this is a test";
stack
  • 10,280
  • 19
  • 65
  • 117
  • 1
    How do you determine it's the 8th character? I ask as this would be a much simpler problem to solve if the character index included the spaces, therefore the `t` you're looking for would actually be the 10th character. – Rory McCrossan Jan 19 '16 at 16:21
  • @RoryMcCrossan oh sorry, it is .. I will edit – stack Jan 19 '16 at 16:22
  • 4
    Please use the search before you ask a new question. – Felix Kling Jan 19 '16 at 16:23
  • 3
    The duplicate question was even right in the ***Related Links*** on right side of this page as well as in the suggestions provided when you created this subject. Sure appears you made no attempt to search for this yourself – charlietfl Jan 19 '16 at 16:26

2 Answers2

1

You can use substr to get the substrings of your string.

var str = "this is a ttest";  // t  h  i  s   i  s   a   t  t  e  s  t
posn = 11; // the position of the first t

//reassemble everything before position 11, and everything after
str = str.substr(0,posn-1) + str.substr(posn,str.length-posn)

//print the output
console.log(str)

Keep in mind you'll need to 'reassemble' the substrings back together after splitting out the character you wanted to replace.

You can see it working here in this JS Fiddle: https://jsfiddle.net/thzhhn4s/

Adam Konieska
  • 2,805
  • 3
  • 14
  • 27
0

To do this you would use substrings:

str = str.substr(0, i) + c + str.substr(i+1);

Where i is the target index and c is the string you are replacing with.

Blubberguy22
  • 1,344
  • 1
  • 17
  • 29