比较Java中具有不同字节顺序掩码的字符串

时间:2018-07-14 00:38:32

标签: java encoding

在我的Java程序中,我有两个字符串s1s2,当它们被打印时,它们看起来都是相等的,但是,由于它们的编码不同,s1.equals(s2)返回的是false。我该如何比较这两个字符串,以便即使它们的编码方式不同,它们仍然相等?

看下面的示例代码:

    s1 = s1.trim();
    s2 = s2.trim();
    byte[] s1bytes = s1.getBytes();
    byte[] s2bytes = s2.getBytes();
    System.out.println(s1+","+s2+","+s1.equals(s2));

    System.out.println("\ns1's bytes are:");
    for (int i = 0; i < s1bytes.length; i++) {
        System.out.println(s1bytes[i]);
    }

    System.out.println("\ns2's bytes are:");
    for (int i = 0; i < s2bytes.length; i++) {
        System.out.println(s2bytes[i]);
    }

此打印:

SHEOGMIOF,SHEOGMIOF,false

s1's bytes are:
-17
-69
-65
83
72
69
79
71
77
73
79
70

s2's bytes are:
83
72
69
79
71
77
73
79
70

如您所见,s1s2打印时看起来是一样的,比较时它们不相等,并且它们的字节数组都不同。

编辑:我的问题与this question不同,因为我不是从文件中读取数据,.java文件中的源代码的编码方式与其他文件中的数据不同。

2 个答案:

答案 0 :(得分:3)

从文件中读取字符串时,请从字符串中删除字节顺序掩码(BOM)。字符代码为"\uFEFF"

public class Foo {
    public static void main(final String[] args) {
        final byte[] b1 = {-17, -69, -65, 83, 72, 69, 79, 71, 77, 73, 79, 70};
        final byte[] b2 = {83, 72, 69, 79, 71, 77, 73, 79, 70};

        final String s1 = new String(b1).replace("\uFEFF", "");
        final String s2 = new String(b2).replace("\uFEFF", "");

        System.out.println(s1);
        System.out.println(s2);
        System.out.println(s1.equals(s2));
    }
}

打印:

SHEOGMIOF
SHEOGMIOF
true

答案 1 :(得分:1)

问题中的样本实际上在编码上并没有不同,但是存在/不存在字节顺序标记。

以下类演示了当字节序列确实表示不同的字符串编码时如何处理这种情况。在示例代码中,必须知道编码。请注意,通常仅从字节序列中推断出编码是一项不平凡的任务。

//  https://stackoverflow.com/questions/229015/encoding-conversion-in-java
//

import java.lang.*;
import java.io.*;
import java.nio.*;

public class encotest {
    public static void main(String[] args) {
        // German lowercase umlauted vowels (äöü) as octet sequences in 2 different encodings
        byte[]  raw_iso8859_15  = { (byte) 0xE4, (byte) 0xF6, (byte) 0xFC };
        byte[]  raw_utf8        = { (byte) 0xC3, (byte) 0xA4, (byte) 0xC3, (byte) 0xB6, (byte) 0xC3, (byte) 0xBC };

        try {
            String s_umlauts_from_iso   = new String(raw_iso8859_15 , "ISO-8859-15");
            String s_umlauts_from_utf8  = new String(raw_utf8       , "UTF-8");

            if (s_umlauts_from_iso.equals(s_umlauts_from_utf8)) {
                System.out.println("They are the same !");
            }
            else {
                System.out.println("They differ!");
            }
        } catch (UnsupportedEncodingException uee) {
            System.out.println("Error: cannot convert");
        }
    }
}

预期输出:

They are the same !