比较两个字符串值并在java中返回其索引

时间:2014-09-26 12:10:15

标签: java string compare substring

我有两个包含一些十六进制值的字符串。

String One = "f9 d4 62 aa f9 d4 62 aa 74 10 99 8b";
String Two = "3c 9c a7 e2 3c 9c a7 e2 b1 58 f9 d4";

请注意,十六进制之间的空格是由将二进制转换为十六进制的函数生成的。此外,十六进制值是成对处理的,例如,f9 d4被视为一对。

我的问题是 - 如何在两个字符串中返回两个相似对的索引(在本例中为子字符串值)? 上面的例子在索引1中有f9 d4,而另一个String在索引6上有它。所以我希望我的输出为(1,6)和(3,6)。 任何帮助或建议将不胜感激。

3 个答案:

答案 0 :(得分:1)

考虑这样的代码:

public static void main(String[] args) {
    String One = "f9 d4 62 aa f9 d4 62 aa 74 10 99 8b";
    String Two = "3c 9c a7 e2 3c 9c a7 e2 b1 58 f9 d4";

    int[] x = convert(One);
    int[] y = convert(Two);

    for (int i = 0; i < x.length; i++) {
        int number = x[i];
        int index = find(number, y);
        if (index > 0) {
            System.out.println("found (" + (i + 1) + "," + index + ")");
        }
    }

}

private static int find(int number, int[] array) {
    for (int i = 0; i < array.length; i++) {
        if (array[i] == number) {
            return i + 1;
        }
    }
    return 0;
}

private static int[] convert(String str) {

    String[] tokens = str.split("\\s");
    int[] result = new int[tokens.length / 2];

    for (int i = 0; i < tokens.length; i += 2) {
        String hex = tokens[i] + tokens[i + 1];
        result[i / 2] = Integer.parseInt(hex, 16);
    }

    return result;
}

输出:

found (1,6)
found (3,6)

正如您所见,convert(str)方法将每个4个十六进制数字转换为1个整数,并返回此类整数的数组。所以convert(One)只是int []等于:

System.out.println(Arrays.toString(x));
[63956, 25258, 63956, 25258, 29712, 39307]

接下来,您可以实现helper方法find(),该方法返回索引,其中在给定数组中找到数字(基于1的索引)。

答案 1 :(得分:1)

希望它有所帮助!

String one = "f9 d4 62 aa f9 d4 62 aa 74 10 99 8b";
String two = "3c 9c a7 e2 3c 9c a7 e2 b1 58 f9 d4";

String[] oneArr = one.split(" ");
String[] twoArr = two.split(" ");

ArrayList<String> results = new ArrayList<>();      

for(int i = 0, countOne = 0 ; i < oneArr.length - 1 ; i = i + 2,countOne++) {

    String hexCoupleOne = oneArr[i] + " " + oneArr[i + 1];

    if(two.contains(hexCoupleOne)) {
        //searching index in two . . .
        for(int j = 0, countTwo = 0  ; j < twoArr.length - 1 ; j = j + 2, countTwo++) {
            String hexCoupleTwo = twoArr[j] + " " + twoArr[j + 1];
            if(hexCoupleOne.equals(hexCoupleTwo))
                results.add((countOne + 1) + "," + (countTwo + 1));
        }           
    }       
}

System.out.println("total pair : "+results.size());
for(String res : results) {
    System.out.println("Found a pair at index="+res.split(",")[0]+" in String one and at index="+res.split(",")[1]+" in String two." );
}

答案 2 :(得分:1)

更有效的方法不是每次都搜索第二个字符串,而是将其转换为更合适的数据结构,例如哈希映射,其中键是字符串对,值是该对出现时的位置列表。当您每次搜索第二个字符串时,此类方法的复杂性为O(n),与O(n 2 )相比。当你有更大的输入字符串时,这种差异将是显着的。

    /**
     * Splits argument into substrings of lengths 6 and puts them into Map
     * where key is substring and value is the list of positions where substring appears 
     * in original string.
     * @param str string to split
     * @return Map of positions
     */
    private static Map<String, List<Integer>> indexMapOfPairs(String str) {
        Map<String, List<Integer>> result = new HashMap<String, List<Integer>>();
        for (int i = 0; i < str.length(); i += 6) {
            String pair = str.substring(i, i + 5);
            List<Integer> indexList = result.get(pair);
            if (indexList == null) {
                indexList = new ArrayList<Integer>(4);
                result.put(pair, indexList);
            }
            indexList.add(i / 6 + 1);
        }
        return result;
    }

    public static void main(String[] args) {
        String one = "f9 d4 62 aa f9 d4 62 aa 74 10 99 8b";
        String two = "3c 9c a7 e2 3c 9c a7 e2 b1 58 f9 d4";

        Map<String, List<Integer>> oneAsMap = indexMapOfPairs(one);
        Map<String, List<Integer>> twoAsMap = indexMapOfPairs(two);

        for (Map.Entry<String, List<Integer>> oneEntry : oneAsMap.entrySet()) {
            String pair = oneEntry.getKey();
            List<Integer> twoIndices = twoAsMap.get(pair);
            if (twoIndices != null) {
                for (Integer oneIndex : oneEntry.getValue()) {
                    for (Integer twoIndex : twoIndices) {
                        System.out.printf("(%d, %d)%n", oneIndex, twoIndex);
                    }
                }
            }
        }
    }