String Manipulation: Spliting Delimitted Data
Posted
by Milli Szabo
on Stack Overflow
See other posts from Stack Overflow
or by Milli Szabo
Published on 2010-06-15T09:50:02Z
Indexed on
2010/06/15
10:02 UTC
Read the original article
Hit count: 345
java
|string-manipulation
I need to split some info from a asterisk delimitted data.
Data Format:
NAME*ADRESS LINE1*ADDRESS LINE2
Rules:
1. Name should be always present
2. Address Line 1 and 2 might not be
3. There should be always three asterisks.
Samples:
MR JONES A ORTEGA*ADDRESS 1*ADDRESS2*
Name: MR JONES A ORTEGA
Address Line1: ADDRESS 1
Address Line2: ADDRESS 2
A PAUL*ADDR1**
Name: A PAUL
Address Line1: ADDR1
Address Line2: Not Given
My algo is:
1. Iterate through the characters in the line
2. Store all chars in a temp variables until first * is found. Reject the data if no char is found before first occurence of asterisk. If some chars found, use it as the name.
3. Same as step 2 for finding address line 1 and 2 except that this won't reject the data if no char is found
My algo looks ugly. The code looks uglier. Spliting using //* doesn't work either since name can be replaced with address line 1 If the data is *Address 1*Address2, split will create two indexes in the array where index 0 will have the value of Address 1 and index 2 will have the value of Address2. Where's the name. Was there a name? Any suggestion?
© Stack Overflow or respective owner