admin管理员组文章数量:1394171
<input type="text" id="personName" />
Given possible values like:
- James
- James Bond
- James Van Bond
- James Van Bond the Very First
I want to learn how to split the value by space, and then obtain the first value as FirstName and all the remaining values as Last name.
Possible? Is split the way to do this?
Thanks
<input type="text" id="personName" />
Given possible values like:
- James
- James Bond
- James Van Bond
- James Van Bond the Very First
I want to learn how to split the value by space, and then obtain the first value as FirstName and all the remaining values as Last name.
Possible? Is split the way to do this?
Thanks
Share Improve this question asked Jan 20, 2011 at 19:35 AnApprenticeAnApprentice 111k202 gold badges637 silver badges1k bronze badges2 Answers
Reset to default 6Example: http://jsfiddle/EpbVc/
var value = $('#personName').val().split(' ');
var firstName = value.shift();
var restOfNames = value.join(' ');
Uses .split()
to split on a single space. If there could be multiple spaces, you could use .split(/\s+/)
.
Then it uses .shift()
to remove the first item from the Array, and assign it to firstName
.
Finally it uses .join()
to join the rest of the Array into a string using a single space as the separator.
use .split(" ") to split into array and then use .join(" ")
本文标签: javascriptParsing an Input value using SplitStack Overflow
版权声明:本文标题:javascript - Parsing an Input value using Split - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744708712a2620996.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论