如何拆分第一; Android中使用全名的姓氏字符串

时间:2014-05-18 05:55:10

标签: android string

我有一个带有全名EditText字段的表单,我想将字符串分解为名字和姓氏字符串。谁可以帮我这个事?我可以知道实现目标的正确方法是什么?

如果用户输入他/她的名字,如A B C。名字将是A&姓氏将BC

我正在尝试这个:

EditText UNSP =(EditText)findViewById(R.id.UserNameToSIGNUP);
    String UserFullName=UNSP.getText().toString();

    String[] arr=UserFullName.split(" ");

    String fname=arr[0];
    String lname=arr[1];

    Log.d("First name",fname);
    Log.d("last name",lname);



    if(UserFullName.length()==0) {

        Toast.makeText(getApplicationContext(), "Submit Name", Toast.LENGTH_SHORT).show();
    }

    else{

         Toast.makeText(getApplicationContext(), "Success", Toast.LENGTH_SHORT).show();


    }

}

7 个答案:

答案 0 :(得分:19)

如果您在不使用数组的情况下只需要姓名的姓和名,这是我认为的最佳方法。名字可以是两个或两个以上的单词,但姓氏总是在现实世界中的一个单词上。

    String name = "Abdul Latif Hussain"
    String lastName = "";
    String firstName= "";
    if(name.split("\\w+").length>1){

       lastName = name.substring(name.lastIndexOf(" ")+1);
       firstName = name.substring(0, name.lastIndexOf(' '));
    }
     else{
       firstName = name;
    }

输出字符串将是: firstName =" Abdul Latif" lastName =" Hussain"

答案 1 :(得分:4)

对于多个名称,最好只为每个字段分别设置EditTexts

对于您的实施,如果您可以保证他们以该格式输入,您可以去:

int firstSpace = UserFullName.indexOf(" "); // detect the first space character
String firstName = UserFullName.substring(0, firstSpace);  // get everything upto the first space character
String lastName = UserFullName.substring(firstSpace).trim(); // get everything after the first space, trimming the spaces off

只是进行一些错误检查以确保格式正确,否则您可能会遇到异常

答案 2 :(得分:2)

请使用此。它将100%工作

str = UNSP.getText().toString();
String[] splited = str.split("\\s+");

答案 3 :(得分:0)

更容易使用String.split(" ").这将创建分隔的字符串,每个字符串在找到" "字符时结束。

答案 4 :(得分:0)

在科特林,我想出了以下解决方案:

val displayName = "John Smith Fidgerold Trump"
var parts  = displayName.split(" ").toMutableList()
val firstName = parts.firstOrNull()
parts.removeAt(0)
val lastName = parts.joinToString(" ")
Log.debug("*** displayName: $displayName")
Log.debug("*** firsteName : $firstName")
Log.debug("*** lastName : $lastName")
Log.debug("**************")    

示例输出:

> ** displayName: John Smith Fidgerold Trump
> ** firsteName : John
> ** lastName   : Smith Fidgerold Trump
> *************
> ** displayName: John Smith Fidgerold
> ** firsteName : John
> ** lastName   : Smith Fidgerold
> *************
> ** displayName: John Smith
> ** firsteName : John
> ** lastName   : Smith
> *************
> ** displayName: John
> ** firsteName : John
> ** lastName   : 
> *************
> ** displayName: 
> ** firsteName : 
> ** lastName   : 
> *************

答案 5 :(得分:0)

仅姓氏:

 public static void getUserFirstName(String fullname){
    String firstName;
    String[] fullNameArray = fullname.split("\\s+");
    if(fullNameArray.length>1) {
        StringBuilder firstNameBuilder = new StringBuilder();
        for (int i = 0; i < fullNameArray.length - 1; i++) {
            firstNameBuilder.append(fullNameArray[i]);
            if(i != fullNameArray.length - 2){
                firstNameBuilder.append(" ");
            }
        }
        firstName = firstNameBuilder.toString();
    }
    else{
        firstName = fullNameArray[0];
    }
}

答案 6 :(得分:0)

科特林版本:

val fullName = "Adriatik Gashi"
val idx = fullName.lastIndexOf(' ')
if (idx == -1) {
      Toast.makeText(context, "Invalid full name", Toast.LENGTH_LONG).show()
      return
}

val firstName = fullName.substring(0, idx)
val lastName = fullName.substring(idx + 1)
Log.e("SPLITED NAME", firstName + " - " + lastName)