重新排列Java中的字符串

用户名

我有一个“ Firstname MiddleInitial Lastname”形式的字符串。

我想将其转换为“ Lastname,Firstname MiddleIntial”

有些名称可能带有中间的首字母缩写,但有些则没有:

String Name1 = "John Papa P";
String name2 = "Michael Jackson";

// Desired Output
result1 = "Papa, John P";
result2 = "Jackson, Michael";

我该怎么做?

竞技场

也许是这样的吗?

public class HelloWorld{

     public static void main(String []args){
        String name1 = "John Papa P";
        String name2 = "Michael Jackson";

        String[] split = name1.split(" ");

        String result;
        if (split.length > 2) {
            result = split[1] + ", " + split[0] + " " + split[2];
        } else {
            result = split[1] + ", " + split[0];
        }

        System.out.println(result);
     }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章