Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove a character at a certain position in a string - javascript [duplicate]

Is there an easy way to remove the character at a certain position in javascript?

e.g. if I have the string "Hello World", can I remove the character at position 3?

the result I would be looking for would the following:

"Helo World" 

This question isn't a duplicate of How can I remove a character from a string using JavaScript?, because this one is about removing the character at a specific position, and that question is about removing all instances of a character.

like image 275
starbeamrainbowlabs Avatar asked Jun 20 '12 09:06

starbeamrainbowlabs


People also ask

How do I remove a character from a string in a position?

You can convert your string to StringBuilder , which have convinient deletaCharAt method. String strWithoutChar = new StringBuilder(yourString). deleteCharAt(yourIndex). toString();

How do I remove a character from a specific index?

The most common approach to removing a character from a string at the specific index is using slicing. Here's a simple example that returns the new string with character at given index i removed from the string s .


2 Answers

It depends how easy you find the following, which uses simple String methods (in this case slice()).

var str = "Hello World";  str = str.slice(0, 3) + str.slice(4);  console.log(str)
like image 154
Matt Avatar answered Sep 23 '22 02:09

Matt


You can try it this way!!

var str ="Hello World"; var position = 6;//its 1 based var newStr = str.substring(0,position - 1) + str.substring(postion, str.length); alert(newStr); 

Here is the live example: http://jsbin.com/ogagaq

like image 40
Ishan Dhingra Avatar answered Sep 19 '22 02:09

Ishan Dhingra