<分区>
我知道在 jQuery 中有几种拆分数组的方法,但我有一个特例: 例如,如果我有这两个字符串:
"G09.4 What"
"A04.3 A new Code"
当我将第一个拆分为 ' '
时,我可以简单地选择前面带有 [0]
的代码,即 G09.4
.当我调用 [1]
时,我得到了文本:What
但是当我对第二个字符串执行相同操作时,我得到了 [1]
A
但我想检索 A new Code
。
那么如何为每个字符串检索代码和单独的文本?
标签 javascript
<分区>
我知道在 jQuery 中有几种拆分数组的方法,但我有一个特例: 例如,如果我有这两个字符串:
"G09.4 What"
"A04.3 A new Code"
当我将第一个拆分为 ' '
时,我可以简单地选择前面带有 [0]
的代码,即 G09.4
.当我调用 [1]
时,我得到了文本:What
但是当我对第二个字符串执行相同操作时,我得到了 [1]
A
但我想检索 A new Code
。
那么如何为每个字符串检索代码和单独的文本?
最佳答案
使用
var someString = "A04.3 A new Code";
var index = someString.indexOf(" "); // Gets the first index where a space occours
var id = someString.substr(0, index); // Gets the first part
var text = someString.substr(index + 1); // Gets the text part
关于javascript - 将字符串分成两部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20474257/
相关文章:
javascript - 如何仅运行此 Angular $interval 函数三次(3 个循环)
javascript - Animate.css 摇动效果每次都不起作用
Javascript 排序功能在 Safari/iPhone 中不起作用
javascript - Google Auth 用户电子邮件不存在
javascript - 我怎样才能从 2 个函数创建一个?
javascript - 我对 `data.filter` 做错了什么
javascript - 在 NodeJs 中,为什么新的 Date 构造函数将默认时间设置为上午 7 点?
javascript - 当 HTMLElement.onclick 事件调用时,在其函数中获取对象的属性 - javascript