我将需要从以星号分隔的数据中分离一些信息。
数据格式为:
NAME*ADRESS LINE1*ADDRESS LINE2
规则:
1. Name should be always present
2. Address Line 1 and 2 might not be
3. There should be always three asterisks.
示例:
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
我的算法是:
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
我的算法看起来很丑陋。代码看起来更加丑陋。使用//*进行分割也不起作用,因为如果数据是*Address 1*Address2,名称可以被地址行1替换。有什么建议吗?
编辑:
尝试使用不带引号的数据“-MS DEBBIE GREEN * 1036 PINEWOOD CRES **”