HARISH
HARISH

Reputation: 179

String manipulation, removing characters until the second occurrence in java script

How to remove all the characters until you encounter second comma(,) in the sentence in js?

I have something like this in my string:

var str = "hello,abc,def,jkl,mno,pqr,stu";

I want the string to contain something like this:

str = "def,jkl,mno,pqr,stu"

How to do something like this?

Is there way to do this other than putting in loop checking for comma then erasing?

Upvotes: 0

Views: 51

Answers (1)

Ziv Ben-Or
Ziv Ben-Or

Reputation: 1194

  1. Convert to array with split.
  2. Cut array with slice
  3. Join the array with join.

var str = "hello,abc,def,jkl,mno,pqr,stu";
var arr = str.split(',');
var newStr = arr.slice(2, arr.length).join(',')
console.log(newStr)

Upvotes: 1

Related Questions