集册 Java实例教程 从全名中获取名字和姓氏数组

从全名中获取名字和姓氏数组

欢马劈雪     最近更新时间:2020-01-02 10:19:05

777
从全名中获取名字和姓氏数组
// 来自 时代Java公众号 - N o w J a  v a . c o m

public class Main {


  public static String[] getFirstNameAndLastNameArrayFromFullName(String fullName) {

    String[] firstnameAndLastnameArr = fullName.split(" ");

    if (firstnameAndLastnameArr.length == 2) {

      return firstnameAndLastnameArr;

    } else if (firstnameAndLastnameArr.length == 1) {

      String firstName = firstnameAndLastnameArr[0];

      firstnameAndLastnameArr = new String[2];

      firstnameAndLastnameArr[0] = firstName;

      firstnameAndLastnameArr[1] = "";

      return firstnameAndLastnameArr;

    } else if (firstnameAndLastnameArr.length > 2) {

      String firstName = firstnameAndLastnameArr[0];

      String lastName = firstnameAndLastnameArr[2];


      firstnameAndLastnameArr = new String[2];

      firstnameAndLastnameArr[0] = firstName;

      firstnameAndLastnameArr[1] = lastName;/**来 自 时   代     Java  公  众  号 - nowjava.com**/

      return firstnameAndLastnameArr;

    } else {

      firstnameAndLas
展开阅读全文