0
Var FullName = John, Cooper;

Where LastName = John and FirstName = Cooper.

How can i split the string FullName and display my two TextField values with LastName and FirstName.

FullName.split(","2);
1
  • 4
    What exactly is the problem (apart from syntax errors)? You know already split so where are you stuck? If you don't know how to use it, look at some examples. Commented Jul 11, 2011 at 13:47

4 Answers 4

5
var splitted = "john, cooper".split(', '),
    lastName = splitted[0],
    firstName = splitted[1];
Sign up to request clarification or add additional context in comments.

Comments

3

First, strings in JavaScript have a " before and after it. Second, it is var and not Var.

Say you have

var FullName = "John, Cooper";

Then you can code like this to split:

var FirstName = FullName.split(", ")[1];
var LastName  = FullName.split(", ")[0];

Comments

2

Just because I like the pattern:

var fullname = "John, Cooper";

(function(first, last) {
    console.log(first, last);
}).apply(null, fullname.split(/,\s*/));

explanation:

The above code creates a function expression (that is done by wrapping the function into the parenthesis). After that it self-invokes that created function immediately by calling .apply() from the Function object (remember, most things in ECMAscript are objects, so are functions). Any function inherits from Function and the .apply() method takes two arguments. 1. a context (=object) for the this parameter in the invoked method and 2. an argumentslist as Array. Since .split() returns an Array we use that to directly pass the result from .split() as arguments.

2 Comments

That's an interesting way, although the first and last name are swapped in the questiom.
Can you explain what this is doing and why someone would use this type of pattern?
0
Var FullName = 'John, Cooper';
name   =  FullName.split(',');
name[0]---//Jhon;
name[1]----//cooper
console.log(FullName.split(','));

3 Comments

Yes, SyntaxError: Unexpected identifier.
dude i got perfectly as said,, wat browser u use...<<<
Chrome, which browser that supports Var and --- do you use?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.