-3

I could not find any usefull post here about my little question.

I have a variable that contains a string and I want it splited into 2 variables.

Example:

var str = "String1;String2";

I want:

var str = "String1;String2";
var string1 = "String1";
var string2 = "String2";
rui404
  • 75
  • 1
  • 11
  • Possible duplicate of [How do I split a string, breaking at a particular character?](http://stackoverflow.com/questions/96428/how-do-i-split-a-string-breaking-at-a-particular-character) – scrappedcola Mar 07 '16 at 18:28

2 Answers2

3
var string1 = str.split(";")[0];
var string2 = str.split(";")[1];

More about split method: Split String Method

juvian
  • 15,875
  • 2
  • 37
  • 38
1

You can use the window object for assigning global variables.

var str = "String1;String2";

str.split(';').forEach(function (a) {
    window[a] = a;
});

document.write(String1 + ' ' + String2);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • Am I missing something here? OP didn't ask anything about global variables or the window object – sg.cc Mar 07 '16 at 18:28
  • 1
    @sg.cc Nina answered the constraint of having named variables available in the scope. But I suppose one could argue that it is an option not to name them at all in the first place. A compromise could be to underline the fact that it may have undesirable side effects. – axelduch Mar 07 '16 at 18:30