문제

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

도움이 되었습니까?

해결책

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

다른 팁

    String fullName = "John Doe";
    String firstName=fullName.substring(0,fullName.indexOf(" "));
    String lastName=fullName.substring(fullName.lastIndexOf(" ")+1);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top