我必须拆分包含全名的字符串,并且需要创建新的名字和姓氏字符串。我用这个来根据名字之间的空格分割全名:
String fullName = "John Doe";
String[] result = fullName.split(" ");
for(String token:result)
System.out.println(token);
我得到了正确的输出," John"和" Doe"。我现在如何使用它来创建
String firstName = "John";
String lastName = "Doe";
由于
答案 0 :(得分:2)
String fullName = "John Doe";
String[] result = fullName.split(" ");
String firstName=result[0];
String lastName=result[1];
答案 1 :(得分:0)
String fullName = "John Doe";
String firstName=fullName.substring(0,fullName.indexOf(" "));
String lastName=fullName.substring(fullName.lastIndexOf(" ")+1);