I have some code which has some perl like behavior and allows me to split a line with some separators onto a head and tail specifying an index. A negative index will start from the end of the string and will make the tail the head, handy if you're not sure on the number of columns in your list.
All works beautiful and as expected however, the split uses regexes to match the column separators, works for ~ but not for "|", which is treated as a regex special character, I may be able to pass in "\\|" but that upsets the join feature. So my question is: is there a way to use split only on normal "string tokens" or are there other clever ways to get divide a string in the documented way.
Thanks in advance. jeroen. My question is there a way to make the split none regex like
Alan Moore
Ranch Hand
Joined: May 06, 2004
Posts: 262
posted
0
Or, if you're running JDK 5 or later,
jeroen dijkmeijer
Ranch Hand
Joined: Sep 26, 2003
Posts: 131
posted
0
Aah the quote functionality was something I was looking for! good thing they put it in 5, too bad I'm bound to 1.4, I'll stick to the replace all functionality. Thanks for the lesson! Jeroen.
Jim Yingst
Wanderer
Sheriff
Joined: Jan 30, 2000
Posts: 18670
posted
0
In JDK 1.4, you can also use
String regex = "\\Q" + separator + "\\E";
That's actually what the later Pattern.quote() uses (unless the regex itself contains \\E internally).
The \Q...\E thing is a carryover from Perl; there's no good reason to use it in Java, considering how much more complicated it is to work with. I've never understood why they used it in the quote() method, but at least we don't have to deal with it.