Search code examples
javaselenium-webdriversplitautomation

How can i separate this "£232.69" value without a currency symbol? I need this value to be 232.69


String Total = driver.findElement(By.xpath("wwwww")).getText();

I will get the Value £232.69 from the above code. Now I want to split the separate the Value without that Currency Symbol. Like 232.69. So that I can compare this value with another one.


Solution

  • This will give you any first number (integer or not, matching expression someNumbers + optional(dot + someNumbers)) occuring in string s as a string:

        String s = "&21.37";
        Pattern p = Pattern.compile("[^0-9]*([0-9]+(\\.[0-9]*)?)");
        Matcher m = p.matcher(s);
        m.matches();
        String s = m.group(1)
    

    You can then extract it as follows:

        Double d = Double.valueOf(s)
    




    Here is updated code: , @kripindas

    String s = "&1,221.37";
    Pattern p = Pattern.compile("[^0-9]*([0-9]*,?([0-9]+(\\.[0-9]*))?)");
    Matcher m = p.matcher(s);
    m.matches();
    String s_num = m.group(1).replace(",", "");
    System.out.println(s_num);
    Double d_num = Double.valueOf(s_num);
    System.out.println(d_num);
    

    Note that this will match:

    "&1,234.56",
    "$123,4.5678",
    "JP234,123.12"
    and convert them to (correspondingly):
    1234.56
    1234.5678
    234123.12