how to parse a line in java

  How do I parse a line in java that is separated by "|" so that I get all the value including empty values


I would like to get 9 elements, the last 4 is empty.. I tried the split command but it returns 5 elements and tokenizer command throw and exception for null values

String[] result = line.split(delims);

Thanks for the help
chand pbAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

String[] result = line.split("\\|");

Open in new window

regex reserved character must be escaped
chand pbAuthor Commented:
Thanks I tried the split command and it does not return the correct arrays... It only return the element with data..

So if i have the string below.. the split command return an array of  5 elements

awking00Information Technology SpecialistCommented:
I ran into this issue once before. The string.replaceAll function doesn't do a string replace but does a regular expression replace. Because regular expressions require the escape character, '\', for the pipe, but that character in itself needs to be escaped in Java, I never seemed to get the correct sequence of pipes and slashes. So I did it in a rather inelegant fashion, but it does work
String str = "0000000556|name1||09/07/2014|456.43||||";
		StringBuilder sb = new StringBuilder();
		char prev = '#';
		for (char c:str.toCharArray()) {
			if (c == '|' && prev == '|') {
				sb.append(" ");
			prev = c;
		String[] result = sb.toString().split("\\|");

Open in new window

Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

Sorry - it should be the following (see API docs)

String[] result = line.split("\\|", -1);

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
chand pbAuthor Commented:
Thank you
awking00Information Technology SpecialistCommented:
Just want to say thanks to CEHJ for the far more elegant method. I never used the limit parameter with replaceAll before and didn't really know it existed until now. As often is the case, reading the API docs provided information on its proper usage :-)
np :)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.