我如何检查一行是否包含特殊字符?

How can i check a Line contains a special chracter?

您好,我在 Linux 系统中存储了一个包含特殊字符 ^C 的文件 像这样:

ABCDEF^CIJKLMN 现在我需要在 java 中读取这个文件并检测是否有这个 ^C 来进行拆分。 要读取 UNIX.I 中的文件必须使用 cat -v fileName 才能看到特殊字符 ^C 的问题,我在其他地方看不到它。 这是我的示例代码。

    InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(this),
            Charset.forName("UTF-8"));

    BufferedReader br = new BufferedReader(inputStreamReader);
    String line;
    while ((line = br.readLine()) != null) {
        if (line.contains("^C")) {
            String[] split = line.split("\" + sepRecord);
            System.out.println(split);

    }

您正在检查该行是否包含 String "^C",而不是 character '^C'(对应于0x03,或 \u0003)。您应该改为搜索字符 0x03。这是适用于您的情况的代码示例:

byte[] fileContent = new byte[] {'A', 0x03, 'B'};
String fileContentStr = new String (fileContent);
System.out.println (fileContentStr.contains ("^C")); // false
System.out.println (fileContentStr.contains (String.valueOf ((char) 0x03))); // true
System.out.println (fileContentStr.contains ("\u0003")); // true, thanks to @Thomas Fritsch for the precision

String[] split = fileContentStr.split ("\u0003");
System.out.println (split.length); // 2
System.out.println (split[0]); // A
System.out.println (split[1]); // B

^C字符显示在Caret Notation中,必须解释为单个字符。