我需要从星号分隔的数据中拆分一些信息。
数据格式:
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**”的数据
最佳答案
您可以使用 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]
);
}
这会打印 ( as seen on 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:
"\\*"
的原因是因为 split
采用正则表达式,而 *
是正则表达式元字符,并且由于您希望它的字面意思,它需要使用 \
进行转义。由于\
本身是一个Java字符串转义字符,要得到字符串中的\
,需要将它加倍。
limit
为 3
的原因是因为您希望数组有 3 个部分,包括尾随的空字符串。默认情况下,limit
的 split
会丢弃尾随的空字符串。
最后一个 *
在执行 split
之前被手动丢弃。
关于java - 字符串操作 : Spliting Delimitted Data,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3044096/