Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
96
rated 0 times [  103] [ 7]  / answers: 1 / hits: 25855  / 8 Years ago, wed, april 6, 2016, 12:00:00

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.


More From » jquery

 Answers
7

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


[#62671] Monday, April 4, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
makaylahh

Total Points: 128
Total Questions: 106
Total Answers: 97

Location: Tanzania
Member since Wed, Feb 24, 2021
3 Years ago
;