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.
You can convert your string to StringBuilder , which have convinient deletaCharAt method. String strWithoutChar = new StringBuilder(yourString). deleteCharAt(yourIndex). toString();
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 .
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)
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With