× The internal search function is temporarily non-functional. The current search engine is no longer viable and we are researching alternatives.
As a stop gap measure, we are using Google's custom search engine service.
If you know of an easy to use, open source, search engine ... please contact support@midrange.com.




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?


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

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
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)

iEYEARECAAYFAk1qd1oACgkQXczQcKdXKg4SJwCgtUjD+TJMFYfWOMHrGSqyZWSL
DYIAnjRaEGMDHha7RcF3o5JnZpndORZ5
=Z6tI
-----END PGP SIGNATURE-----
--
This is the Java Programming on and around the IBM i (JAVA400-L) mailing
list
To post a message email: JAVA400-L@xxxxxxxxxxxx
To subscribe, unsubscribe, or change list options,
visit: http://lists.midrange.com/mailman/listinfo/java400-l
or email: JAVA400-L-request@xxxxxxxxxxxx
Before posting, please take a moment to review the archives
at http://archive.midrange.com/java400-l.



As an Amazon Associate we earn from qualifying purchases.

This thread ...

Follow-Ups:
Replies:

Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

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.