I want to remove the few characters in a string using index. for example: My input is: "5,4,3,2,1" I want to remove the 1th to 2nd index position characters(here , to 4). The Output should be 5,3,2,1. Is there any predefined function in jquery or javascript to done this?
2 Answers
You can try to use substring function like this:
var mystring = "5,4,3,2,1";
alert( mystring.substr(0, 1) + mystring.substr(3));
Comments
I would juse use javascripts split function for that.
So if you have
var string = "5,4,3,2,1";
than you just need to do
var splitted = string.split(",");
whereas the character in the brackets is the one you want to split on. After you did that, you can just make a new string, and build it with the array elements.
So you do something like
var string2 = splitted[0] + "," + splitted[2] + "," + splitted[3] + "," splitted[4];
1 Comment
kalles
Thank you for try to help me.
str = str.substr(0, 1) + str.substr(3)slice?slice