Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split string by word

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.

like image 388
Becky Avatar asked Oct 26 '25 10:10

Becky


1 Answers

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

like image 65
callback Avatar answered Oct 28 '25 02:10

callback



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!