sscanf equivalent in Java [duplicate]

2019-05-04 16:07发布

问题:

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

I'm pretty new to Java, but seeing how useful the sscanf function is, I wonder if there is an equivalent in Java. I've got many pretty much formatted strings (namely, writing scripting language for my purposes), so regular expressions are overkill here.

回答1:

Scanner scanner = new Scanner ("2 A text 3 4");
scanner.nextInt (); // 2
scanner.next ();    // A 
scanner.next ();    // text
scanner.nextInt (); // 3
scanner.nextInt (); // 4
// and so on.

Here is the official documentation of the Scanner class.