开发者

String fullname Split java

I created a program which will parse the firstName, middleName and lastName. Here is the program and output. This program can definitely be improved and need some input on reducing my cumbersome ugly code and replace it with a better one. Any suggestions or example ?

public class Test {

  public static void main(String[] args) {

    String fullName = "John King IV. Cena";
    String[] tokens = fullName.split(" ");
    String firstName = "";
    String middleName = "";
    String lastName = "";
    if(tokens.length > 0) {
        firstName = tokens[0];
        middleName = tokens.length > 2 ? getMiddleName(tokens) : "";
        lastName = tokens[tokens.length -1];
  开发者_开发技巧  }
    System.out.println(firstName);
    System.out.println(middleName);
    System.out.println(lastName);
  }

  public static String getMiddleName(String[] middleName){
      StringBuilder builder = new StringBuilder();
      for (int i = 1; i < middleName.length-1; i++) {
          builder.append(middleName[i] + " ");
      }

      return builder.toString();
  }
}

John King IV. Cena


This code does the same, but doesn't keep a trailing space in the middle name. This is one of several possible cleaner implementations.

public class Test {

    public static void main(String[] args) {

        String name = "John King IV. Cena";

        int start = name.indexOf(' ');
        int end = name.lastIndexOf(' ');

        String firstName = "";
        String middleName = "";
        String lastName = "";

        if (start >= 0) {
            firstName = name.substring(0, start);
            if (end > start)
                middleName = name.substring(start + 1, end);
            lastName = name.substring(end + 1, name.length());
        }    

        System.out.println(firstName);
        System.out.println(middleName);
        System.out.println(lastName);
    }
}

As the guys said, next time go directly to https://codereview.stackexchange.com/


The algorithm will fail if the persons last name has more than one word, like Abraham Van Helsing. Van is not a middle name but part of the last name.

Obviously, there is no algorithm to clearly distinguish between middle name and last name in general. We always have to guess and we can only try to improve the probability that the guess is correct, maybe be checking middle name parts against word or filter lists.


You could also use a StringTokenizer for this:

import java.util.StringTokenizer;

public class Test {

  public static void main(String[] args) {

    String fullName = "John King IV. Cena";

    StringTokenizer stok = new StringTokenizer(fullName);
    String firstName = stok.nextToken();

    StringBuilder middleName = new StringBuilder();
    String lastName = stok.nextToken();
    while (stok.hasMoreTokens())
    {
      middleName.append(lastName + " ");
      lastName = stok.nextToken();
    }

    System.out.println(firstName);
    System.out.println(middleName.toString().trim());
    System.out.println(lastName);
  }
}


Update the code to handle where there is no last name i.e. user enters only the first name like "Mark"

if(tokens.length > 0) {
    firstName = tokens[0];
    middleName = tokens.length > 2 ? getMiddleName(tokens) : "";
    if(tokens.length > 1){
        lastName = tokens[tokens.length -1];
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜