【问题标题】:Partially compare 2 strings部分比较 2 个字符串
【发布时间】:2018-09-16 07:36:07
【问题描述】:

我有一个带有 GUI 的程序,我在其中插入几个十六进制(字符串),按下搜索按钮,然后找到它的代码。我从 CSV 文件中导入我有代码的所有十六进制。我想要的是,如果我输入例如:11 D7 E2 FA,我的程序将只搜索第二个半字节,x 意味着被忽略:x1 x7 x2 xA,如果它在 CSV 中找到类似的东西,它会给我它的代码。这是我到目前为止所拥有的,这只在字符串匹配时找到我的情况。

codeOutputField.setText("");
String input = hexEntryField.getText();

try {
    br = new BufferedReader(new FileReader(FIS));
    while ((line = br.readLine()) != null) {
        code = line.split(csvSplitBy);
        if (input.equals(code[0])) {
            codeOutputField.setText(code[1]);
        }
    }
}

CSV 示例:

01 5F 1E CE,0055
01 5F 13 D0,0062
01 5E 36 FE,0101
00 5E 36 FF,1002

这是现在对我有用的代码,想分享它。我现在唯一的问题是我只能从bat 文件运行jar 文件,双击不起作用。我不知道为什么。

String input = hexEntryField.getText();
String[] myStringArray = input.split("");

codeOutputField.setText("");

try {
    br = new BufferedReader(new FileReader(FIS));
    while ((line = br.readLine()) != null) {
        code = line.split(csvSplitBy);
        List<String> items = Arrays.asList(code[0].split(""));
        System.out.println(items);

        if (myStringArray[1].equals(items.get(1))
                && myStringArray[4].equals(items.get(4))
                && myStringArray[7].equals(items.get(7)) 
                && myStringArray[10].equals(items.get(10))) {
            codeOutputField.setText(code[1]);
        }
    }
}

【问题讨论】:

    标签: java string compare string-comparison partial


    【解决方案1】:

    您可以使用谓词链来比较十六进制字符串搜索字符串中的偶数个字符:

    // assume that we have an array of hexes
    String[] hexes = {"015F1ECE", "015F13D0", "015E36FE", "005E36FF"};
    // user input
    String search = "035D3EFA";
    // conditions to check
    Predicate<String> predicateChain = IntStream
            // even numbers 0, 2, 4, 6
            .range(0, 4).map(i -> i * 2)
            // compare even characters from the
            // current string and the search string
            // Stream<Predicate<String>>
            .mapToObj(i -> (Predicate<String>)
                    str -> str.charAt(i) == search.charAt(i))
            // reduce a stream of predicates
            // to a single predicate chain
            .reduce(Predicate::and)
            // otherwise the condition is not met
            .orElse(p -> false);
    
    // output the filtered array
    Arrays.stream(hexes).filter(predicateChain).forEach(System.out::println);
    

    输出:

    015E36FE
    005E36FF
    

    另见:Looking for similar strings in a string array

    【讨论】:

      【解决方案2】:

      您的问题更多是关于解析每一行。我会结合一些正则表达式:

      public class Record {
      private static final Pattern HEX_VALUE = Pattern.compile("[A-F0-9][A-F0-9]");   
      //...
      public static Record from(String line) throws Exception {
          Record record = new Record();
          String[] parts = line.split(",");
          if (parts.length != 2) {
              throw new Exception(String.format("Bad record! : %s", line));
          }
          record.code = parts[1];
          String hexes[] = parts[0].split("\\s");
          if (hexes.length != 4) {
              throw new Exception(String.format("Bad record! : %s", line));
          }
          for (String hex: hexes) {
              if (!HEX_VALUE.matcher(hex).matches()) {
                  throw new Exception(String.format("Bad record! : %s", line));
              }
          }
          record.hex1 = hexes[0];
          record.hex2 = hexes[1];
          record.hex3 = hexes[2];
          record.hex4 = hexes[3];
          return record;
      }
      ...
      @Override
      public boolean equals(Object obj) {
          boolean ret = false;
          if (obj instanceof Record) {
              Record r = (Record) obj;
              ret = equalsSecondCharacter(this.hex1, r.hex1) 
                      && equalsSecondCharacter(this.hex2, r.hex2) 
                      && equalsSecondCharacter(this.hex3, r.hex3) 
                      && equalsSecondCharacter(this.hex4, r.hex4);
          }
      
          return ret;
      
      }
      ...
      

      然后只需搜索记录列表即可。在示例中,我使用了 Apache Commons Collections 过滤:

      while((line = br.readLine()) != null) {
          records.add(Record.from(line));
      }
      // Check into the list
      Collection<Record> filtered = 
      CollectionUtils.select(records, new EqualPredicate<Record>(inputRecord));
      System.out.println("Results:");
      for (Record rec: filtered) {
          System.out.println(rec);
      }
      

      希望对你有用。

      【讨论】:

        【解决方案3】:

        假设代码被声明为字符串代码[2],你必须得到你想要的让“csvSplitBy”等于“,”

        或明确地:

        code = line.split(",");
        

        【讨论】:

          猜你喜欢
          • 2012-05-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-02-14
          • 2019-11-10
          • 1970-01-01
          • 1970-01-01
          • 2023-01-09
          相关资源
          最近更新 更多