kalles
kalles

Reputation: 337

how to Remove the characters in between a string using index in jquery or javascript?

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?

Upvotes: 1

Views: 3131

Answers (2)

Rahul Tripathi
Rahul Tripathi

Reputation: 172458

You can try to use substring function like this:

var mystring = "5,4,3,2,1";
alert( mystring.substr(0, 1) + mystring.substr(3));

JSFIDDLE DEMO

Upvotes: 2

nameless
nameless

Reputation: 1521

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

Upvotes: 1

Related Questions