按字分割字串 [英] Split string by word

查看:41
本文介绍了按字分割字串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多个分配给具有共同词的变量的字符串.如何将这些字符串从普通单词分为两部分?

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";

不是必须多次编写子字符串,有没有办法从 4A 拆分字符串并获得以下结果?

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.

推荐答案

您可以使用:

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);

请参阅文档: https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/String/split

这篇关于按字分割字串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆