如何将 Java 中的字符串从全名到姓氏、名字分开?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25984389/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 09:05:52  来源:igfitidea点击:

How can I separate a string in Java from full name to last name, first name?

javastring

提问by John

How would you take a string that you have input from the keyboard and rearrange it? For example, in my case I am asking the user to import a person's name in "last name, first name" format. I then have to change that to "first name last name".

你如何从键盘输入一个字符串并重新排列它?例如,在我的例子中,我要求用户以“姓,名”格式导入一个人的名字。然后我必须将其更改为“名字姓氏”。

Here is what I have so far:

这是我到目前为止所拥有的:

private void setName() {
    Scanner in = new Scanner(System.in);
    System.out.println("Please enter the last name followed by the first name of" +
            "a student: ");
    name = in.nextLine();
}

采纳答案by DucRP

A simple solution would be to ask for each name separately, using two calls to Scanner's nextLine() function.

一个简单的解决方案是分别询问每个名称,使用对 Scanner 的 nextLine() 函数的两次调用。

import java.util.Scanner;

public class FirstNameLastName {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("Please enter the student's last name: ");
        String lastName = scan.nextLine();

        System.out.println("Please enter the student's first name: ");
        String firstName = scan.nextLine();

        System.out.println("Hello, " + firstName + " " + lastName);
    }
}

回答by giscard.faria

Well, you can use the String.split(" ") which returns a String[]. After that all you have to do is run this array on reverse order printing the strings.

好吧,您可以使用返回 String[] 的 String.split(" ")。之后你所要做的就是以相反的顺序运行这个数组打印字符串。

String[] tokens = name.split(" "); // split line on string separated by " "
for(int i = tokens.length - 1; i >= 0; i--)
    System.out.println(tokens[i]);

PS: This isn't about java, this is about programming.

PS:这不是关于java,这是关于编程。

Regards.

问候。

回答by Blake Yarbrough

Consider creating two inputs as below

考虑创建两个输入如下

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println("Please enter the last name of a student: ");
        String lastName = in.nextLine();

        System.out.println("Please enter the first name of a student: ");
        String firstName = in.nextLine();

        System.out.println(firstName + " " + lastName);
    }
}

回答by rgamber

Use name.split(delimiter). This returns a String array, and each element the array is a component of the String separated by a delimiter that you have to specify as a parameter while using the method. Eg. From official Java documentation:

使用name.split(delimiter). 这将返回一个字符串数组,该数组的每个元素都是字符串的一个组成部分,由分隔符分隔,您必须在使用该方法时将其指定为参数。例如。来自官方 Java 文档

The string "boo:and:foo", for example, yields the following results with these expressions:

Regex   Result
:   { "boo", "and", "foo" }
o   { "b", "", ":and:f" }

Approach:
Scanner.nextLine()returns a String. So that makes the variable namea String instance. Now you need to find out what methods are supported by String, so that you can invoke it on the instance using a .. This is the time to go through the official Java documentation for String, and find out the methods that you can use. Once you find the methods you want, you can Google for examples :).

方法:
Scanner.nextLine()返回一个字符串。所以这使变量name成为 String实例。现在您需要找出 String 支持哪些方法,以便您可以使用.. 现在是阅读String的官方Java 文档并找出可以使用的方法的时候了。一旦你找到你想要的方法,你可以谷歌搜索示例:)。