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];
0
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];
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
  • That's an interesting way, although the first and last name are swapped in the questiom.
    – pimvdb
    Commented Jul 11, 2011 at 13:58
  • Can you explain what this is doing and why someone would use this type of pattern?
    – jfriend00
    Commented Jul 11, 2011 at 15:12
0
Var FullName = 'John, Cooper';
name   =  FullName.split(',');
name[0]---//Jhon;
name[1]----//cooper
console.log(FullName.split(','));
3
  • Yes, SyntaxError: Unexpected identifier.
    – pimvdb
    Commented Jul 11, 2011 at 13:58
  • dude i got perfectly as said,, wat browser u use...<<<
    – K6t
    Commented Jul 11, 2011 at 14:12
  • Chrome, which browser that supports Var and --- do you use?
    – pimvdb
    Commented Jul 11, 2011 at 14:13

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.