what is the Java equivalent of sscanf for parsing values from a string using a known pattern?

后端 未结 8 861
梦如初夏
梦如初夏 2020-11-27 06:01

So I come from a C background (originally originally, though I haven\'t used that language for almost 5 years) and I\'m trying to parse some values from a string in Java. In

8条回答
  •  鱼传尺愫
    2020-11-27 06:34

    None of these examples were really satisfactory to me so I made my own java sscanf utility:

    https://github.com/driedler/java-sscanf/tree/master/src/util/sscanf

    Here's an example of parsing a hex string:

    String buffer = "my hex string: DEADBEEF\n"
    Object output[] = Sscanf.scan(buffer, "my hex string: %X\n", 1);
    
    System.out.println("parse count: " + output.length);
    System.out.println("hex str1: " + (Long)output[0]);
    
    // Output:
    // parse count: 1
    // hex str1: 3735928559
    

提交回复
热议问题