-1

我如何解析这个字符串,

=w115113298015

这样我只能得到等号后的前 13 个字符。

和这个字符串

 c5|208971|YAhj56|thoimn|10/8/79|T|=w11511329801500 

这样我就得到了最后一个等号之后的前 13 个字符;

有时这个字符串不会有等号,

 c5|208971|YAhj56|thoimn|10/8/79|T|w11511329801500 

我需要最后一个 | 后面的前 13 个字符 符号。

所以我从两个字符串中得到 w115113298015 并将它们相互比较。

我使用此代码来匹配相等的字符串,但我现在使用的字符串不同。任何输入都会有所帮助。

String firstContents = intent.getStringExtra("first_contents")
String secondContents = intent.getStringExtra("second_contents")

if (firstContents.equals(secondContents)) { 
   rest of code here.

如何向其中添加代码以解析字符串?

谢谢

4

4 回答 4

1

这会拆分字符上的字符串|并获取最后一部分。如果最后一部分的第一个字符是=,则将其删除。然后将生成的字符串修剪为长度为 13。

    String input = "c5|208971|YAhj56|thoimn|10/8/79|T|=w11511329801500";
    String[] parts = input.split("\\|");
    String comparableLastPart = parts[parts.length-1];

    if (comparableLastPart.charAt(0) == '=') {
        comparableLastPart = comparableLastPart.substring(1);
    }

    if (comparableLastPart.length() > 13) {
        comparableLastPart = comparableLastPart.substring(0, 13);
    }

    System.out.println("comparableLastPart: " + comparableLastPart);
于 2013-11-06T05:40:01.137 回答
0

试试这个像魅力一样的作品

public String parseString(String input) {

        if (input != null) {
            if (input.contains("=")) {
                String arr[] = input.split("=");
                return arr[arr.length - 1];
            } else {
                String arr[] = input.split("\\|");
                return arr[arr.length - 1];
            }
        }else{
            return "";
        }
    }

如何使用?

System.out.println(parseString("=w11511329801500"));
        System.out.println(parseString("c5|208971|YAhj56|thoimn|10/8/79|T|=w11511329801500"));
        System.out.println(parseString("c5|208971|YAhj56|thoimn|10/8/79|T|w11511329801500"));
于 2013-11-06T05:49:03.177 回答
0

您可以通过以下方式执行此操作。

  1. 查找=使用该indexOf(char)方法的索引。
  2. 如果它等于-1,则=符号不存在。

    一个。使用方法查找|char的最后一个索引。lastIndexOf(char)

    湾。从此索引中,使用substring(startIndex, endIndex)方法从实际字符串中提取一个子字符串(从最后一个索引|到接下来的 13 个字符)。

  3. 如果 index of=不等于-1,则使用方法从实际字符串中提取一个子字符串substring(startIndex, endIndex)(从 index of=到接下来的 13 个字符)。

于 2013-11-06T05:38:09.450 回答
0

尝试这个...

    String s = "c5|208971|YAhj56|thoimn|10/8/79|T|w11511329801500";
    String string = null;
    if (s.contains("=")) {
        string = s.substring(s.lastIndexOf("=") + 1);
    } else {
        string = s.substring(s.lastIndexOf("|") + 1);
    }
    if (string.length() >= 13) {
        string = string.substring(0, 13);
    }
于 2013-11-06T05:44:02.743 回答