user2630764
user2630764

Reputation: 622

Contcatenating split strings in javascript

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.

Upvotes: 0

Views: 44

Answers (3)

AsgarAli
AsgarAli

Reputation: 2211

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

Upvotes: 2

Steve
Steve

Reputation: 9571

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

Upvotes: 0

Kartikeya Khosla
Kartikeya Khosla

Reputation: 18873

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

Upvotes: 3

Related Questions