Reputation: 10469
I'm importing a file with umpteen lines of "##,##"
. Each number can be one or two digits.
I'd like to use String.split(regex)
to get the two numbers without the adjacent quote marks.
Understanding that I could nibble off the first and last character and use a non-regex split, I'm hoping that there is a regular expression that will make this more graceful.
Suggestions?
EDIT:
In: "12,3"
Out: 12
3
Upvotes: 0
Views: 195
Reputation: 88707
You could split at the quotes as well but that would result in an array of length 4. Unfortunately, there's no way of splitting a string and removing others characters from the same string in one call using String#split
.
As an alternative, you could use Apache's StringUtils
:
String[] n = StringUtils.removeStart( StringUtils.removeEnd( "##,##", "\""), "\"").split(",");
Edit: as a side note, using StringUtils
would allow for missing quotes at the start or end of the input string. If you're sure they're always present, a simple substring(...)
might be sufficient. (credits go to @Ingo)
Upvotes: 0
Reputation: 13749
How about using a regexp \"(d+),(d+)\"
. Then using Pattern.matcher(input)
instead of String.split
, and obtaining your digits by Matcher.group(int)
.
Please consider following snippet:
String line = "\"1,31\"";
Pattern pattern = Pattern.compile("\"(\\d+),(\\d+)\"");
Matcher matcher = pattern.matcher(line);
if (matcher.matches()) {
int firstNumber = Integer.parseInt(matcher.group(1));
int secondNumber = Integer.parseInt(matcher.group(2));
// do whatever with the numbers
}
Upvotes: 7
Reputation: 3125
You can remove all double-quotes characters in each line then split the string by ,
String toSplit = "\"##,##\"";
String[] splitted = toSplit.replaceAll("\"", "").split(",");
Using \"
in the toSplit
string to simulate the "##,##"
string.
Upvotes: 2