Search code examples
androidandroid-studiomultiautocompletetextview

Android MultiAutoCompleteTextView truncate string


I have a string MultiAutoCompleteTextview as:

VACUTAINER [3X10] - 30RS, VACUTAINER[4 X10] - 40RS

So here, I want to split the string after (-) and I also want to truncate the last two characters (RS) and get the string as 30 40 and want to sum this 30 and 40 and get the answer in another EditText.

Thank You.


Solution

  • Try this code

    String[] givenString={"VACUTAINER [3X10] - 30RS","VACUTAINER[4 X10] - 40RS"};
                //or use loop
                String newStringFirst=givenString[0].substring(givenString[0].lastIndexOf("-") + 1).trim();
                String newStringSecond=givenString[1].substring(givenString[1].lastIndexOf("-") + 1).trim();
                String thirtyString=newStringFirst.substring(0,newStringFirst.length()-2);
                String fortyString=newStringSecond.substring(0,newStringSecond.length()-2);
                System.out.println(Integer.parseInt(thirtyString)+Integer.parseInt(fortyString));