To parse strings that have the same format, just use the java.util.regex
package, create the correct regular expression by pulling out the necessary data. Pattern and Matches are responsible for pattern and matcher, respectively.
As an example:
String ISBN = "ISBN: 123-456-789-112-3, ISBN: 1234567891123"; Pattern pattern = Pattern.compile("(\\d-?){13}"); Matcher matcher = pattern.matcher(ISBN); while (matcher.find()) System.out.println(matcher.group());
pull out
123-456-789-112-3 1234567891123
All results will be in the form of lines. Therefore, after drawing up the regulars correctly, if you need to convert to a type, then we use the appropriate methods for these classes. For example:
String testString = "666"; int testInteger = Integer.parseInt(testString);
or
String testString = "666.666"; double testDouble = Double.parseDouble(testString);
If the fractional number has a comma 666,666
, and a dot separator is needed, then you can use java.text.NumberFormat
NumberFormat format = NumberFormat.getInstance(Locale.FRANCE); Number number = format.parse("1,234"); double d = number.doubleValue();
Split
. Otherwise, use the regulars. Without an example, we cannot know what you need. - Denisjava.util.regex
package, write the regulars pattern and find a match viaMatcher
.......... docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html .. .... as an exampleString ISBN = "ISBN: 123-456-789-112-3, ISBN: 1234567891123"; Pattern pattern = Pattern.compile("(\\d-?){13}"); Matcher matcher = pattern.matcher(ISBN); while (matcher.find()) System.out.println(matcher.group());
String ISBN = "ISBN: 123-456-789-112-3, ISBN: 1234567891123"; Pattern pattern = Pattern.compile("(\\d-?){13}"); Matcher matcher = pattern.matcher(ISBN); while (matcher.find()) System.out.println(matcher.group());
............ it remains for you to create your own regulars for the data that you want to pull out and forward - Alexey Shimansky