java - 字符串操作 : Spliting Delimitted Data

标签 java string

我需要从星号分隔的数据中拆分一些信息。

数据格式:

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字符串转义字符,要得到字符串中的\,需要将它加倍。

limit3 的原因是因为您希望数组有 3 个部分,包括尾随的空字符串。默认情况下,limitsplit 会丢弃尾随的空字符串。

最后一个 * 在执行 split 之前被手动丢弃。

关于java - 字符串操作 : Spliting Delimitted Data,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3044096/

相关文章:

java - 将 ID 分配给 JPA native 查询结果的最简单方法是什么?

java - 有人可以解释一下这个错误是什么意思吗?

Python:从列表转换为字符串

java - 如何在循环中对字符串进行重复添加?

c - 为什么在C中copy chars时出现 "@"added

java - JUnit - assertFalse 不排除 == 运算符?

java - 如何使用构建器模拟内部变量

java - 网络客户端 + jackson : how to setup deserialization to convert snake_case into camelCase?

ruby-on-rails - 如何将这两个 Ruby 字符串测试结合到一个正则表达式中?

java - Android中解析字符串