javaandroidstringstringreader

How to read numbers after followed by particular Character of a string using Java Android?


String str = "[amt is E234.98.valid 23/12/2013.Sample text]"

How to read only the amount value from the above sample string which is near after to the character "E".


Solution

  • You can use regexp and matching groups to extract what you need.

    CODE update:

    Use by calling ParseWithRegexp.testIt()

    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    public class ParseWithRegexp {
        static String str = "[amt is E234.98.valid 23/12/2013.Sample text  E134.95.valid 23/12/2015]";
        public static void testIt() {
            Pattern p = Pattern.compile("E([0-9.]+)\\.valid");
            Matcher m = p.matcher(str);
            while (m.find()) {
                System.out.println("found: "+m.group(1));
            }
        }
    }
    

    Upadte: You can be more strict with the regexp, for example: "E([0-9]+\\.[0-9]+)"