Question

I have to split a string containing a full name and need to create new first name and last name strings. I used this to split the full name based on the space between names:

  String fullName = "John Doe";
       String[] result = fullName.split(" ");
       for(String token:result)
            System.out.println(token);

I get the correct output, "John" and "Doe". How do I now use that to create

String firstName = "John";
String lastName = "Doe";

Thanks

Était-ce utile?

La solution

String fullName = "John Doe";
String[] result = fullName.split(" ");
String firstName=result[0];
String lastName=result[1];

Autres conseils

    String fullName = "John Doe";
    String firstName=fullName.substring(0,fullName.indexOf(" "));
    String lastName=fullName.substring(fullName.lastIndexOf(" ")+1);
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top