-1

I have a string as var a = "000020097613 / 0020";. How can I get the string in two different variables which are currently separated by / now?

I want as below:

var b = "000020097613";
var c = "0020";
user3916007
  • 189
  • 1
  • 9
  • You could google for "JavaScript split string", which would give you [this first result](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split), followed by the SO question this one is marked as a duplicate of. What was the place your were confused? Did you not know how to split the string, or did you not know how to assign the results of splitting to variables? –  Dec 04 '16 at 05:34
  • However, there may also be basic things you don't know the name for, and don't know how to search for. Asking every time on SO is not really a good solution here--SO is supposed to be for interesting programming problems, not a sort of chat room to answer newbie question. Nothing against newbies; that's just the nature of SO. I would suggest you sit down and read a good book, or tutorial, or even the [MDN page about strings](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String). Any of these will give you a good overview of everything you can do. –  Dec 04 '16 at 06:00
  • (cont'd) Then you'll know what's available, and can easily get on with your work without stopping to ask SO every time you need to know how to upper-case a string. –  Dec 04 '16 at 06:00
  • By the way, what do you want to do if no `/` character is found in the input? What if there is more than one? –  Dec 04 '16 at 06:01

2 Answers2

0

How about:

var res = a.split(" / ");
var b = res[0], c = res[1];

Or equivalently in ES6:

var [b, c] = a.split(" / ");
Derek 朕會功夫
  • 92,235
  • 44
  • 185
  • 247
0

Try this ES6 solution.

let [b, c] = '000020097613 / 0020'.split(' / ');

The [b, c] syntax is called Array destructuring. It's a part of the destructuring assignment syntax.

Lewis
  • 14,132
  • 12
  • 66
  • 87