0

I am brand new at programming, especially JS. I seem to be stuck on a split string.

I need to split a string into two separate strings. I know I can do so with the slice and substr like below, which is my sample I have of what I know. I am assuming my name is Paul Johnson. with below I know that if I have an output of first and last name with the parameters I setup, I will have Paul as my first name and Johnson as my second.

 var str = document.getElementById("fullName").value;
 var firstname = str.slice(0, 4);
 var lastname = str.substr(4, 13); 

My question is that I am getting hung up on how to find the space and splitting it from there in order to have a clean cut and the same for the end. Are there any good resources that clearly define how I can do that?

Thanks!

asked Feb 24, 2014 at 21:57
2
  • So you're asking how to split a string on a space character? Commented Feb 24, 2014 at 21:59
  • maybe devdocs.io is a nice place to search functions and theri meanings devdocs.io/javascript Commented Feb 24, 2014 at 22:01

6 Answers 6

3

What you're after is String split. It will let you split on spaces. http://www.w3schools.com/jsref/jsref_split.asp

var str = "John Smith";
var res = str.split(" ");

Will return an Array with ['John','Smith']

answered Feb 24, 2014 at 22:00
Sign up to request clarification or add additional context in comments.

1 Comment

I tried this previously, but I did not want the "," to occur. I want the names to be in their own 'placeholder' for first name and last name.
1

str.indexOf(' ') will return the first space

answered Feb 24, 2014 at 22:00

1 Comment

i would actually caution against that because it can return -1 and screw up the proceeding slice() command...
1

There is a string split() method in Javascript, and you can split on the space in any two-word name like so:

var splitName = str.split(" ");
var firstName = splitName[0];
var lastName = splitName[1];

http://www.w3schools.com/jsref/jsref_split.asp

answered Feb 24, 2014 at 22:00

1 Comment

I tried split, but it came back [object HTMLSpanElement]. Not sure what I did incorrectly. var str = document.getElementById("fullName").value; var splitName = str.split(" "); var firstName = splitName[0]; var lastName = splitName[1];
1

A good way to parse strings that are space separated is as follows:

pieces = string.split(' ')

Pieces will then contain an array of all the different strings. Check out the following example:

string_to_parse = 'this,is,a,comma,separated,list';
strings = string_to_parse.split(',');
alert(strings[3]); // makes an alert box containing the string "comma"
answered Feb 24, 2014 at 22:04

Comments

1

Use str.split().

The syntax of split is: string.split(separator,limit)

split() returns a list of strings.

The split() function defaults to splitting by whitespace with to limit.

Example:

var str = "Your Name";
var pieces = str.split();
var firstName = pieces[0];
var lastName = pieces[1];

pieces will be equal to ['Your', 'Name'].

firstName will be equal to 'Your'.

lastName will be equal to 'Name'.

answered Feb 24, 2014 at 22:00

1 Comment

I tried this previously, but I did not want the "," to occur. I want the names to be in their own 'placeholder' for first name and last name.
0

I figured it out: var str = document.getElementById("fullName").value; var space = str.indexOf(" ");
var firstname = str.slice(0, space); var lastname = str.substr(space);

Thank you all!

answered Feb 26, 2014 at 15:00

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.