4

I have multiple strings assigned to variables which has a common word. How can I split these strings in to two parts from the common word?

var str= "12344A56789";

Instead of having to write substring multiple times, is there a way to split the string from 4A and get the following results?

var first = "1234";
var second = "56789";

NOTE: Each string lengths are different.

Becky
  • 5,467
  • 9
  • 40
  • 73
  • 7
    `str.split('4A')` – Rayon Apr 06 '16 at 12:43
  • 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) – briosheje Apr 06 '16 at 12:52

1 Answers1

13

You can do it using :

var str= "12344A56789";
var splitted = str.split('4A'); //this will output ["1234", "56789"]
var first = splitted[0]; //"1234"
var second = splitted[1]; //"56789"
console.log('First is: ' + first + ', and second is: ' + second);

see docs: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split

andbuk
  • 5
  • 2
callback
  • 3,981
  • 1
  • 31
  • 55