0

Hi,

I have a string of following format:

string arr = "a-b-c";

which is not constant which can be

"a-b-c-d";

I want the output as :

string result = "b-c" 

or

"b-c-d-....";

I am using string.split("-") but not sure how to skip first element.

3 Answers 3

3

Use .substring() and .indexOf() as shown :-

var arr = "a-b-c-d";
alert(arr.substring(arr.indexOf('-') + 1));

var arr = "a-b-c-d-e-f";
alert(arr.substring(arr.indexOf('-') + 1));

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

Comments

2

var str = "a-b-c-d-e-f-g-h-i"; 
var res = str.slice(2);
alert(res) ;

Comments

0

You could use substring to skip the first two characters, then your normal `string.split("-") to get the rest into an array.

Eg:

var input = "a-b-c-d-e";
var removeFirstChar = input.substring(2);
var splitChars = removeFirstChar.split("-");

This is assuming you always want to skip the first letter and it's hyphen.

JSFiddle

Comments

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.