String Manipulation: Splitting Delimitted Data
- by Milli Szabo
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 was *Address 1*Address2. Any suggestion?