On a scale from 1 to 10 the issue I talk today about is about 2… unless it will bite your behind and make you randomly scream WTF’s for two hours:) So today’s motto is: beware of small things! At first take a look at the following code snippet:
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: |
public class LineParser {
private final String[] values;
public LineParser(String line, String separator) {
values = line.split(separator);
}
public String getValue(int index) {
return values[index];
}
}
|
It’s a simple class that encapsulates parsing a text line and stores the result. Simple right? Two out of ten? If you think so then try to answer the following: what will the following code print?
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: |
public static void main(String[] args) {
LineParser parser1 = new LineParser("A,B,C", ",");
System.out.println("parser1:" + parser1.getValue(1));
LineParser parser2 = new LineParser("A B C", " ");
System.out.println("parser2:" + parser2.getValue(1));
LineParser parser3 = new LineParser("A|B|C", "|");
System.out.println("parser3:" + parser3.getValue(1));
LineParser parser4 = new LineParser("A\\B\\C", "\\");
System.out.println("parser4:" + parser4.getValue(1));
}
|
Well… in every of the parsers we intend to split a simple three-value line and print out the second value. But what gets printed? For the first and second parser there is no surprise: the second value is ‘B’ and that’s exactly what gets printed. The third one instead of a second value prints ‘A’ – the first one… If that’s not strange enough the last parser throws an exception! That’s really unexpected
So where’s the catch? What’s wrong? Some of you already knew it, some probably start to suspect it… It’s all because of String.split() method – instead of taking a separator String as a parameter (which I tried to silently imply in the code) it takes a regular expression. Because of that two last parsers failed – both pipe and backslash signs have special meaning in Java regexps!
Mystery solved, so problem is gone… is it really? Of course you might be tempted just to fix the snippet above by writing the regexps correctly – this would be fine for this code. Now go home and check your code: do you use user-provided values in String.split()? What about String.replaceAll()? If you do you might be in real trouble… The real lesson is that some of the String methods take as a parameter plain Strings (eg: String.regionMatches()) while other expect a String with a regular expression (eg: String.matches()). Beware and double check!
4 Comments until now
Pattern.quote() is The Way to handle these.
jakarta commons StringUtils.split() works as expected and (since it does not use regex) performs much faster.
Pattern.split is actually faster, so just scrap the “commons”.
Women’s vibram five fingers kso shoes Gray Pink Shoes can keep your feet in the freest condition,just as if your were barefooted. vibram five fingers kso shoes is made of the best and proper materials,so you don’t need to doubt of its comfort with so thin sole and upper.
Gray match with a dreamlike and romantic color,pink vibram five fingers kso shoes is very consistent with the style of young girls.In this busy and tense times,people need relax without any fetter.
Add your Comment!