最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Parsing an Input value using Split - Stack Overflow

programmeradmin1浏览0评论
<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 badges
Add a ment  | 

2 Answers 2

Reset to default 6

Example: 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(" ")

发布评论

评论列表(0)

  1. 暂无评论