0

I have to remove 1.1 from list [test, 1.1 test1, test, tsest]. I have tries the following code but its not working

List<String> li = new ArrayList<>();
    for(WebElement el : element)
    {

        String str = el.getText();
        if(str.contains("0-9"))
        {
        String intValue = str.replace("[0-9]", " "); 
        li.add(intValue);
        }else
        {
            li.add(str);

        }
3
  • 3
    First of all, your code won't compile. So, post your compiled code here not junk code. Is it specific to one type of data or it is based on some pattern ? Commented Jul 25, 2018 at 5:48
  • Please show us more data so that we can form a general rule for what you want to remove. Commented Jul 25, 2018 at 5:50
  • @Ravi please have a look the code now Commented Jul 25, 2018 at 6:54

3 Answers 3

1

Here is what you can do:

List<String> strValues = Arrays.asList("test", "1123.12345 test1",
            "test", "tsest"); // Storing all the values in the List
    List<String> li = new ArrayList<>(); // Creating a new list which will store the updated values of the string

    for (String str : strValues) { // Iterating through the list

        if (str.matches("^[0-9].*$")) { // Checking whether the string starts with a number
            String newString = str.replaceAll("[-+]?([0-9]*\\.[0-9]+)", ""); // This regular expression matches an optional sign, that is either followed by zero or more digits followed by a dot and one or more digits
            li.add(newString); // Adding new string in the list

        }

        else {
            li.add(str); //Adding the old string if the string doesn't start with a number
        }

    }

    for (String newValue : li) {
        System.out.println(newValue); // printing the list
    }

}
Sign up to request clarification or add additional context in comments.

1 Comment

Please follow the link: meta.stackexchange.com/questions/5234/…. It is a good etiquette to accept the answer when someone in StackOverflow helps you because they take out their special time for solving your problems.
0

You can do it with regex, and Matcher, Pattern combination, and put it all in some collection, in this case list:

public static void main (String[]args){

    List<Double> listOfNumbers = new ArrayList<>();

    Pattern p = Pattern.compile("\\d*\\.?\\d+");

    Matcher m = p.matcher("Here is a sample with 2.1 and others 1 3 numbers and they are less than number 12");
    while (m.find()) {
        listOfNumbers.add(Double.valueOf(m.group()));
    }

    for (double num :listOfNumbers) {
        System.err.println(num);

    }
}

output is:

2.1
1.0
3.0
12.0

Hope this helps,

Comments

0

You can use below regex for that:

\\d*\\.?\\d+

Output:

jshell> String str = "1.1 some data"
str ==> "1.1 some data"

jshell> str = str.replaceAll("\\d*\\.?\\d+", "");
str ==> " some data"

Description:

\d - Any number 
* - 0 or more occurence
\. - Search for specific symbol .
? - zero or one occurence
+ - one or more occurence

3 Comments

can you please explain me the meaning of this parameter "\\d*\\.?\\d+", ""
Sorry i mistakenly put thw wrong code, here is the code i am using contains method for finding the integer part List<String> li = new ArrayList<>(); for(WebElement el : element) { String str = el.getText(); if(str.contains("0-9")) { String intValue = str.replace("[0-9]", " "); li.add(intValue); }else { li.add(str); }
@YashodipPatil the shared regex will search for any integer or decimal and will replace it with "" I have also updated the regex description in my answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.