Split string by word

寵の児 提交于 2021-02-04 19:16:27

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/36451512/split-string-by-word

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!