-3

This is my original string,

required:true,validType:'timegt['#timeofdaymeterslotonebegintime,#timeofdaymeterslotoneendtime']

I want to split into two. the output will be like

required:true 
 validType:'timegt['#timeofdaymeterslotonebegintime,#timeofdaymeterslotoneendtime']

Can someone help me out with this.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Karthikeyan Pandian
  • 357
  • 4
  • 8
  • 18
  • 1
    You write code, we (maybe) try help fix it. We're not here to write code for you. – Marc B Jun 21 '16 at 14:15
  • 3
    Perhaps [`string.split(",", 2)`](https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#split%28java.lang.String,%20int%29) would do? – aioobe Jun 21 '16 at 14:18
  • FYI, JavaScript's String type has a `split` method that works just like Java's. [See here](http://www.w3schools.com/jsref/jsref_split.asp). – callyalater Jun 21 '16 at 15:11
  • Is your problem more general than your single example ? is it by any chance something like http://stackoverflow.com/questions/632475/regex-to-pick-commas-outside-of-quotes ? – Touffy Jun 21 '16 at 15:52

1 Answers1

1

first solution you split the string to obtain the two values in an Array :

var str = "required:true,validType:'timegt['#timeofdaymeterslotonebegintime,#timeofdaymeterslotoneendtime']";
var arr = str.split(",");
var result = [];
result.push(arr[0]);
result.push(arr.filter((element, index) => (index>0)).join());
console.log(result);

second solution you extract from the initial string two strings containing your values :

var str = "required:true,validType:'timegt['#timeofdaymeterslotonebegintime,#timeofdaymeterslotoneendtime']";
var index = str.indexOf(",");
var result1 = str.slice(0, index);
var result2 = str.slice(index+1);
console.log(result1);
console.log(result2);
kevin ternet
  • 4,514
  • 2
  • 19
  • 27