0

I am getting a response from the server as below,

<p style="font-family:Arial, Helvetica, sans-serif; font-size:18px;"> <strong>XXX Pvt Ltd. has been used 3 times.The contact information for XXX can only be viewed -4 more times on App until it expires. Please urgently verify the information.</strong></p>

Here I would need the integer value -4 if it is negative or 4 if it is positive. In case it is negative I would like to get it with the negative sign.

Is there any way to clip all the text values and just get a part of the response from the server in android?

I actually need the count of times we can see the info above in my app.

I tried researching and gone through some links as,

Remove all occurrences of \ from string

How to replace the characher `\n` as a new line in android

How to strip or escape html tags in Android

However, none of this could actually help me in achieving what I want.

Please, can anyone help?

manini
  • 358
  • 1
  • 4
  • 14
  • Have you looked at Regular Expressions? If this is the string you receive you could simply apply a regEx on that to filter out the needed informations. – Nico Oct 26 '17 at 08:34
  • @Nico Can u please advise what regex shall I use in this case? – manini Oct 26 '17 at 08:39

1 Answers1

2

You can get the last digit from a String using this:

String s = "";
String str = "<p style=\\\"font-family:Arial, Helvetica, sans-serif; font-size:18px;\\\"> <strong> This is the information you need 5 times you can see.</strong></p>";
Pattern p = Pattern.compile("(\\d+)(?!.*\\d)");
Matcher m = p.matcher(str);
if (m.find()) {
    s = m.group();
}
int number = Integer.parseInt(s);

number is the number from your html string.

See: https://regex101.com/r/jAFJiL/1

Edit, multiple numbers:

Here is a small ugly piece of code I wrote for you. This gets all numbers from a String and removes the first one. (The 18px from your input):

String str = "<p style=\"font-family:Arial, Helvetica, sans-serif; font-size:18px;\"> <strong>XXX Pvt Ltd. has been used 3 times.The contact information for XXX can only be viewed 4 more times on App until it expires. Please urgently verify the information.</strong></p>";
boolean previousWasDigit = false;

ArrayList<Integer> numbers = new ArrayList<>();
String number = "";
for (int i = 0; i < str.length(); i++) {
    char c = str.charAt(i);
    if (Character.isDigit(c)) {
        if (previousWasDigit) {
            number += c;
        } else {
            number = Character.toString(c);
        }
        previousWasDigit = true;
    } else {
       if (previousWasDigit) {
           numbers.add(Integer.parseInt(number));
       }
       previousWasDigit = false;
    }
}
numbers.remove(0); // we remove the first digit (18) here
// numbers contains the digits of your string
gi097
  • 7,313
  • 3
  • 27
  • 49
  • Thanks a lot for the response, However, I am getting "illegal escape character in string literal" error here. ANy idea why would that happen? – manini Oct 26 '17 at 08:57
  • Hey thanks the pattern seems fine but I got another error in the line result = m.group(); It says Incompatble types. Required Boolean Found String. – manini Oct 26 '17 at 09:04
  • Incompatble types. Required Boolean. Found String. result is string but it requires boolean – manini Oct 26 '17 at 09:06
  • There is one more issue I am facing here... in case I am having 2 numbers at 2 different places.. the result is appending both the numbers. Please see the edited question – manini Oct 26 '17 at 09:47
  • What do you mean? You have `18` and `5` in your string, and my regex only gets `5`. – gi097 Oct 26 '17 at 09:48
  • It working but sometimes it's appending the numbers. Like In case I have the string as in my question I get 34. – manini Oct 26 '17 at 10:00
  • Hey I am facing an issue again here.. in case I am having my number in negative like -7, the regex is only returning 7. Is there any way I can get the minus as well when it's in the negative? – manini Oct 30 '17 at 08:45