Java code to parse an input CharSequence, controlled by a format string from a java.util.Formatter.
Similar in concept to a "scanf", but the format String syntax is different. This code is mainly intended to be used with fixed records.
Say you created a String with the following code
String format = "This is my data: %5d%7x%7o%4s%4.2f values in %%";
String result = String.format(format, 1234, 0x1234, 01234, "1234", 1234.56);
result = "This is my data: 1234 1234 123412341234.56 values in %"
To read back the information:
Unformatter unf = new Unformatter(format);
List<Object> scanned = unf.unformat(result);
scanned will contain the following:
- Long 1234
- Long 4660 (same as hex 0x1234)
- Long 668 (same as octal 01234)
- String "1234"
- Double 1234.56
- CharSequence ""
All parsed values are returned as Longs, Doubles, or Strings as appropriate.
The last element (5 in the above example) represents any remaining data in the input that was not parsed. This is useful if you are reading from a CharSequence containing multiple records.
Note: Since this if for reading fixed formats, all conversions should include a width!
- d decimal, returned as a Long
- f floating point, returned as a Double
- h hex, returned as a Long
- o octal, returned as a Long
- s string, returned as a String
- % not really a conversion, but "%%" in the format is converted to fixed text "%"
- n not really a conversion, but "%n" in the format is converted to a fixed text newline
- any conversions not noted above, including uppercase
- argument indexing
- flags (sorry, no locale-specific grouping for now)
For more, consult the code or the zipped javadocs
- constructor, taking a format string
- parses input,
- returns a List contain Longs, Doubles, or Strings for the data.
- the last element in the List is a CharSequence of any reminaing input.