-4

I want use register in my application and i should send password and verifyCode with SMS to users phones.
But i should read verifyCode from message and set automatically number into verifyCode EditText.

My message format :

Hi, welcome to our service.

your password
12345

your verifyCode
54321

How can i do it? Please help me <3

  • Possible duplicate of http://stackoverflow.com/questions/30862162/otp-token-should-be-automatically-read-from-the-message – Sagar Gangawane Jan 07 '17 at 07:27
  • 2
    Possible duplicate of [OTP (token) should be automatically read from the message](http://stackoverflow.com/questions/30862162/otp-token-should-be-automatically-read-from-the-message) – Kaushal28 Jan 07 '17 at 07:28
  • @SagarGangawane, i want read from string file! please help me for this – user7386713 Jan 07 '17 at 07:30
  • @Kaushal28, i want read from string file! please help me for this – user7386713 Jan 07 '17 at 07:30
  • read from string file? or read string from file? – Kaushal28 Jan 07 '17 at 07:31
  • @Kaushal28, i want read VerifyCode from string. for example : above text write into String message = "Hi, Welcome..."; fetch 54321 from verifyCode and show it into TextView – user7386713 Jan 07 '17 at 07:34
  • every time the string is fixed and only password and verify code is changing? – Kaushal28 Jan 07 '17 at 07:36
  • look at here - all thing that you need - http://stackoverflow.com/questions/34950808/how-to-extract-numbers-from-textview-android-studio – Saeid Jan 07 '17 at 07:37

2 Answers2

0

Assuming that the number of digits are fixed in password and verify codes (Generally they are same as default values), We can extract digits from the string and then find substring which has verify code. This assumption is for simplicity.

String numberOnly= str.replaceAll("[^0-9]", "");
String verifyCode = numberOnly.substring(6);

Here String verifyCode = numberOnly.substring(6); is getting last 5 digits of the string which is your verification code. You can also write numberOnly.substring(6,10); to avoid confusions.

But this is prone to errors like StringIndexOutOfBoundsException, So whenever you want to get substring which is starting from index i till the end of the string, always write numberOnly.substring(i).

Kaushal28
  • 4,823
  • 4
  • 30
  • 57
0

There are a lot ways to do this. You can use some complicated regex or use a simple spilt method.

Try this,

    String str = "Hi, welcome to our service.\n"
            + "\n"
            + "your password \n"
            + "12345\n"
            + "\n"
            + "your verifyCode \n"
            + "54321";

    // Solution #1
    String[] parts = str.split("\n");
    System.out.println(parts[3]);
    System.out.println(parts[6]);

    // Solution #2
    String PAT = "(password|verifyCode)\\s+(\\d+)";
    Pattern pats = Pattern.compile(PAT);
    Matcher m = pats.matcher(str);
    while (m.find()) {
        String grp = m.group(2);
        System.out.println(grp);
    }
K Neeraj Lal
  • 6,560
  • 3
  • 21
  • 32