Ranjit
Ranjit

Reputation: 25

How to remove specific String stored in an ArrayList

I have a code which stores the values for later use. But I need to find the exact names and not the whole. I am getting output like below:

Output result shows: [ Ranjit Nayak ( Attorney ), ranjti Nyak ( Case Manager ), shenoy attorney ( Parallegal ) ]

So I need to use some method to remove content including brackets. Please help.

In the code: CaseManagersreceivingreminders is a global ArrayList, I have a similar another ArrayList stored values. Once the string is edited successfully I need to compare both the array values.

Tried trim, replace. Nothing works.

    List<WebElement> PMPageCMList =
        driver.findElements(By.xpath("//div[@id='collapseCM']/div[2]/div[2]/div"));

    int totalcms = PMPageCMList.size();
    for (int i = 1; i <= totalcms; i++) {
        CaseManagersreceivingreminders.add(
            driver
                .findElement(By.xpath("//div[@id='collapseCM']/div[2]/div[2]/div" + "[" + i + "]" + "/span"))
                .getText()
        );
    }

    Collections.sort(CaseManagersreceivingreminders);
    Collections.sort(ReminderCMsList);

    try {
        boolean isEqual = CaseManagersreceivingreminders.contains(ReminderCMsList);
        System.out.println(isEqual);
        Log.pass("Data matching" + isEqual);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

I should get the output like: [ Ranjit Nayak, ranjti Nyak, shenoy attorney ]

Upvotes: 1

Views: 290

Answers (4)

Samuel Philipp
Samuel Philipp

Reputation: 11042

You can remove the (...) part from each string using the following regex: \s*\(.*\)

If you want to update the existing list you can use List.replaceAll():

Pattern pattern = Pattern.compile("\\s*\\(.*\\)");
list.replaceAll(s -> pattern.matcher(s).replaceAll(""));

Alternatively you can use Java Streams if you want to create a new list with the results:

Pattern pattern = Pattern.compile("\\s*\\(.*\\)");
List<String> result = list.stream()
        .map(s -> pattern.matcher(s).replaceAll(""))
        .collect(Collectors.toList());

Upvotes: 0

H&#252;lya
H&#252;lya

Reputation: 3433

You can use regex to remove (...) part of the String:

List<String> newList =  new ArrayList<>();

for(String s: CaseManagersreceivingreminders) {
    s = s.replaceAll("\\(.*\\)", "");
    newList.add(s);
}

System.out.println(newList);

Output:

[Ranjit Nayak, ranjti Nyak, shenoy attorney]

About \\(.*\\) regex:

  • \\( and \\) : For any special characters like ( or ) you should use \\ before them to match.
  • .* : Matches any character zero or more times.

Thus \\(.*\\) expression can match part of a string like ( Attorney )

Upvotes: 1

Bohemian
Bohemian

Reputation: 424993

Use the replaceAll() method of List, with a suitable UnaryOperator:

list.replaceAll(s -> s.replaceAll(" *[(].+?[)]", ""));

Upvotes: 0

RohanJ
RohanJ

Reputation: 616

Your Collection CaseManagersreceivingreminders is a list of strings, so just iterate over the list, for each string split over '(' and save the new strings in a new list.

for (String names : CaseManagersreceivingreminders) {
    CaseManagersreceivingreminders_New.add(names.split("(")[0]);
}

Upvotes: 0

Related Questions