1

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?

5
  • Try str = str.substr(0, 1) + str.substr(3) Commented Oct 14, 2015 at 6:01
  • How about using slice? Commented Oct 14, 2015 at 6:02
  • @SandeepNayak It's saved in a string, not in an array, so you can't use slice Commented Oct 14, 2015 at 6:07
  • @nameless It can be used on string as well. Check developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Oct 14, 2015 at 6:08
  • thank you for try to help me.. Commented Oct 14, 2015 at 6:19

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

1

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

Thank you for try to help me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.