StringUtils.countMatches () not working for char tab
I am checking a csv file with content like:
TEST;F;12345;0X4321 - 1234 DUMMYTEXT;0X4321 - 1234 TESTTEXT
Until now, the values have been split by ';' and the method worked like a charm:
private static final String COLUMN_SEPARATOR = ";";
public void validateFile(BufferedReader reader) {
String line = reader.readLine();
while (line != null && result == ValidationResult.VALID) {
//this is broken with tab-stop as COLUMN_SEPARATOR
int matches = StringUtils.countMatches(line, COLUMN_SEPARATOR);
if (matches != getCSVColumnCount() - 1
&& StringUtils.isNotBlank(line)) {
if (matches == 0) {
//MISSING_CSV_COLUMN_SEPERATOR;
} else {
//UNEXPECTED_CSV_COLUMN_COUNT;
}
}
line = reader.readLine();
}
}
As a modified requirement, I now have to treat tabs as column separators while text can contain spaces:
TEST F 12345 0x4321 - 1234 DUMMYTEXT 0x4321 - 1234 TESTTEXT
I changed the following line:
private static final String COLUMN_SEPARATOR = "\\t";
Problem: StringUtils.countMatches(line, "\\t")
Can't find any occurrences (returns 0). I don't want to do:
int matches = line.split("\\t").length;
since I am a supervist that this will be a significant success (csv files are not small). Do you know what is best to do?
source to share
You have hidden the backslash in your Java string literal. Thus, the resulting string consists of two characters: a backslash and a "t".
To represent a tab character in a Java string literal, use \t
(note the one backslash).
Correction:
private static final String COLUMN_SEPARATOR = "\t";
Then it StringUtils.countMatches()
will work as you expect.
source to share