我需要从星号分隔的数据中拆分一些信息。
数据格式:
NAME*ADRESS LINE1*ADDRESS LINE2
Run Code Online (Sandbox Code Playgroud)
规则:
1. Name should be always present
2. Address Line 1 and 2 might not be
3. There should be always three asterisks.
Run Code Online (Sandbox Code Playgroud)
样品:
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
Run Code Online (Sandbox Code Playgroud)
我的算法是:
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
Run Code Online (Sandbox Code Playgroud)
我的算法看起来很丑。代码看起来更丑。使用 //* 进行拆分也不起作用,因为如果数据是 *Address 1*Address2,则名称可以替换为地址行 1。有什么建议吗?
编辑:
尝试使用不包括引号“-MS DEBBIE GREEN*1036 PINEWOOD CRES**”的数据
您可以使用String[] split(String regex, int limit)如下:
String[] tests = {
"NAME*ADRESS LINE1*ADDRESS LINE2*",
"NAME*ADRESS LINE1**",
"NAME**ADDRESS LINE2*",
"NAME***",
"*ADDRESS LINE1*ADDRESS LINE2*",
"*ADDRESS LINE1**",
"**ADDRESS LINE2*",
"***",
"-MS DEBBIE GREEN*1036 PINEWOOD CRES**",
};
for (String test : tests) {
test = test.substring(0, test.length() - 1);
String[] parts = test.split("\\*", 3);
System.out.printf(
"%s%n Name: %s%n Address Line1: %s%n Address Line2: %s%n%n",
test, parts[0], parts[1], parts[2]
);
}
Run Code Online (Sandbox Code Playgroud)
打印内容(如 ideone.com 上所示):
NAME*ADRESS LINE1*ADDRESS LINE2*
Name: NAME
Address Line1: ADRESS LINE1
Address Line2: ADDRESS LINE2
NAME*ADRESS LINE1**
Name: NAME
Address Line1: ADRESS LINE1
Address Line2:
NAME**ADDRESS LINE2*
Name: NAME
Address Line1:
Address Line2: ADDRESS LINE2
NAME***
Name: NAME
Address Line1:
Address Line2:
*ADDRESS LINE1*ADDRESS LINE2*
Name:
Address Line1: ADDRESS LINE1
Address Line2: ADDRESS LINE2
*ADDRESS LINE1**
Name:
Address Line1: ADDRESS LINE1
Address Line2:
**ADDRESS LINE2*
Name:
Address Line1:
Address Line2: ADDRESS LINE2
***
Name:
Address Line1:
Address Line2:
-MS DEBBIE GREEN*1036 PINEWOOD CRES**
Name: -MS DEBBIE GREEN
Address Line1: 1036 PINEWOOD CRES
Address Line2:
Run Code Online (Sandbox Code Playgroud)
的原因"\\*"是因为split需要一个正则表达式,并且*是一个正则表达式元字符,并且由于您希望它按字面意思表示,因此需要使用 进行转义\。由于\它本身是Java字符串转义字符,因此要\在字符串中获取a,您需要将其加倍。
limit使用of的原因3是因为您希望数组由 3 个部分组成,包括尾随空字符串。limit默认情况下,-less会split丢弃尾随的空字符串。
最后一个在执行*之前被手动丢弃split。
| 归档时间: |
|
| 查看次数: |
253 次 |
| 最近记录: |