Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用indexOf和substring以及compareTo交换名称_Java_String_Swap - Fatal编程技术网

Java 使用indexOf和substring以及compareTo交换名称

Java 使用indexOf和substring以及compareTo交换名称,java,string,swap,Java,String,Swap,我很难理解如何使用indexOf()和substring()和compareTo()方法在数组中翻转人名和姓氏。Yo可以使用split方法将人名分隔成一个字符串数组,因为它们是由空格分隔的 例如,让我们考虑一个名称: String name = "Mario Freitas"; String[] array = name.split(" "); // the parameter is the string separator. In this case, is the space for(S

我很难理解如何使用
indexOf()
substring()
compareTo()
方法在数组中翻转人名和姓氏。

Yo可以使用split方法将人名分隔成一个字符串数组,因为它们是由空格分隔的

例如,让我们考虑一个名称:

String name = "Mario Freitas";

String[] array = name.split(" "); // the parameter is the string separator. In this case, is the space

for(String s : array){
    System.out.println(s);
}
此代码将在不同的行中打印每个名称(因为字符串是分开的)

然后,您可以使用equals方法比较分开的名字和姓氏

假设有2个字符串数组,通过split方法获得,每个字符串都有一个不同的人名

public void compare names(String name1, String name2){
    String array1[] = name1.split(" ");
    String array2[] = name2.split(" ");

    if(array1[0].equals(array2[0])){
        System.out.println("First names are equal");
    }

    if(array1[1].equals(array2[1])){
        System.out.println("Second names are equal");
    }
}

假设你有以下几点:

String[] names = new String[]{"Joe Bloggs", "Sam Sunday"};
您可以使用以下代码交换姓和名:

for (int i=0; i < names.length; i++)
{
    String someName = names[i];
    int spaceBetweenFirstAndLastName = someName.indexOf(" ");
    //These next two lines of code may be off by a character
    //Grab the characters starting at position 0 in the String up to but not including
    //   the index of the space between first and last name
    String firstName = someName.substring(0, spaceBetweenFirstAndLastName);
    //Grab all the characters, but start at the character after the space and go 
    //   until the end of the string
    String lastName = someName.substring(spaceBetweenFirstAndLastName+1);
    //Now, swap the first and last name and put it back into the array
    names[i] = lastName + ", " + firstName;
}
for(int i=0;i

字符串
compareTo
方法现在可以通过比较一个名称与另一个名称来对名称进行排序,因为姓氏是字符串的开头。看看api,看看你是否能找到答案。

你可以使用正则表达式,特别是对名字和姓氏进行重新排序

    String[] names = { "John A. Doe", "James Bond" };
    for (int i = 0; i < names.length; i++) {
        names[i] = names[i].replaceAll("(.*) (.*)", "$2, $1");
        System.out.println(names[i]);
    }
然而,如果你交换的唯一原因是因为你以后想按姓氏排序,那么你实际上根本不需要交换。只需将姓氏提取代码封装到一个helper方法中(因此它是可测试的、可重用的,等等),然后在您的自定义代码中使用它即可


请注意,在这两个代码段中,它都是用于定义名字和姓氏之间边界的空格字符的最后一个索引。

能否发布您试图解决的问题的完整描述?indexOf、substring和compareTo并不是真正用来改变数组中的内容,听起来你正在试图解决一些更大的问题。你能详细说明一下这个问题吗?这个数组中有什么值?这是一个我遇到麻烦的类的程序。我有一个名字数组,里面有名字和姓氏,我们的教授试图向我们解释如何使用indexof来找到名字和子字符串之间的空间,还有competo,我也不确定它是如何组合在一起的,例如String[]name={“Mary Smith”,“John Cummings”,“Herald Williams”,“Debbie Boile”,“Ralph Chester”};我需要把姓氏换成三个名字,这样我以后就可以按姓氏排序了。如果你对学习很认真,试试看;它为初学者提供了数百个练习,包括关于基本
String
操作的部分。所有名称都已存储在字符串数组中。我需要翻转它们并将它们放回相同的数组。您可以使用split方法翻转名称。只需创建另一个字符串,用翻转的数组的值填充它。然后你就有了翻转的名字。例如:字符串s=array1[1]+“”+array1[0];
    String[] names = { "John A. Doe", "James Bond" };
    for (int i = 0; i < names.length; i++) {
        names[i] = names[i].replaceAll("(.*) (.*)", "$2, $1");
        System.out.println(names[i]);
    }
import java.util.*;

//...

static String lastName(String name) {
    return name.substring(name.lastIndexOf(' '));
}

//...

String[] names = { "John A. Doe", "James Bond" };       
Comparator<String> lastNameComparator = new Comparator<String>() {
    @Override public int compare(String name1, String name2) {
        return lastName(name1).compareTo(lastName(name2));
    }           
};
Arrays.sort(names, lastNameComparator);
for (String name : names) {
    System.out.println(name);
}
James Bond
John A. Doe