|
- --
Hi Pete,
The pipe ( | ) character has to be escaped with a backslash. But guess
what? The backslash character also has to be escaped. So you need to make
the split delimiter "\\|".
This is the output of the following code:
1|20110226||||0|6068|6|33243.95|6062|35084323.55|0|0.00|0|0.00
[1, 20110226, , , , 0, 6068, 6, 33243.95, 6062, 35084323.55, 0, 0.00, 0,
0.00]
===========================
import java.util.*;
public class xxxSplit
{
public static void main( String[] args )
{
String[] as;
String s =
"1|20110226||||0|6068|6|33243.95|6062|35084323.55|0|0.00|0|0.00",
sDelimiter = "\\|";
as = s.split( sDelimiter );
System.out.println( s );
System.out.println( Arrays.toString( as ) );
} // end main
} // end class xxxSplit
===========================
HTH,
Joe Sam
Joe Sam Shirah - http://www.conceptgo.com
conceptGO - Consulting/Development/Outsourcing
Java Filter Forum: http://www.ibm.com/developerworks/java/
Just the JDBC FAQs: http://www.jguru.com/faq/JDBC
Going International? http://www.jguru.com/faq/I18N
Que Java400? http://www.jguru.com/faq/Java400
----- Original Message ----- From: "Pete Hall" <pete@xxxxxxxxxxxxxx>
Newsgroups: midrange.java400-l
To: <java400-l@xxxxxxxxxxxx>
Sent: Sunday, February 27, 2011 11:10 AM
Subject: Re: Is String.split() broken?
On 2011-02-27 08:11, David Gibbs wrote:
On 2/26/2011 5:29 PM, Pete Hall wrote:
Why does this work:
String[] fields = errRecord.replace("|", "\t").split("\t");
While this does not:
String[] fields = errRecord.split("|");
Isn't '|' a regex operator?
Have you tried escaping the |? ("\|")
david
Yeah, tried that. It throws an exception if I ignore the editor error:
Invalid escape sequence (valid ones are \b \t \n \f \r \" \' \\).
I've tried "\0x7c", "\u007C" and "\u7C00" too. The editor accepts those,
but they all return an array of String containing one element, which
includes all of the characters in the record, including the pipes. I
copied the pipe character from the variable window into V (hex viewer),
and it really is 0x7c. I even tried extracting the pipe from the record
with substring() and using the extracted delimiter in split(). It made
no difference.
An example errRecord looks like this:
1|20110226||||0|6068|6|33243.95|6062|35084323.55|0|0.00|0|0.00
I guess I'll just go with the two stage parse. Apparently split() has
some limitations.
-- Pete Hall
pete@xxxxxxxxxxxxxx
As an Amazon Associate we earn from qualifying purchases.
This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].
Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.