-1

How do I parse this string,

=w115113298015

so that I get only the first 13 characters after the equals sign.

and this string

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

so that I get the first 13 characters after the last equals sign;

sometimes this string wont have the equals sign ,

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

and I'll need the first 13 characters after the last | sign.

So I get the w115113298015 from both strings and compare them with each other.

I used this code to match the strings which were equal, but the strings I'm using now are different. Any input would be helpful.

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

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

How do I add code to this to parse the string ?

Thanks

1
  • 2
    What did you tried your self? Nov 6, 2013 at 5:26

4 Answers 4

1

This splits the string on the | character and gets the last part. If the first character of the last part is =, it is removed. The resulting string is then trimmed to be of length 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);
0

try this...

    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);
    }
0

You could do this in the following way.

  1. Find the index of = using the indexOf(char) method.
  2. If its equal to -1, then = symbol is not present.

    a. Find the last index of | char using lastIndexOf(char) method.

    b. From this index, extract a substring from the actual string using substring(startIndex, endIndex) method(from the last index of | to the next 13 characters).

  3. If index of = is not equal to -1, then extract a substring from the actual string using substring(startIndex, endIndex) method(from the index of = to the next 13 characters).

0

Try this works like charm

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 "";
        }
    }

How to use?

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"));

Not the answer you're looking for? Browse other questions tagged or ask your own question.