1

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 ]

4
  • then you have to walk through each element of the array and replace the element with the modified one. Where is the problem now? Or just create a new array based on the new elements, doesn't matter here for small arrays Commented Jun 27, 2019 at 12:10
  • @AKSW sample code please! Commented Jun 27, 2019 at 12:27
  • How can split() and trim() related to Selenium? Am I missing something? Commented Jun 27, 2019 at 13:10
  • You should stick to the Java Naming Conventions: variable names are always written in camelCase. Commented Jun 27, 2019 at 22:00

4 Answers 4

1

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 )

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

Comments

0

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]);
}

2 Comments

but this will only remove 1st (, how about the content inside the bracket?
@ranjitnayak "Ranjit Nayak ( Attorney )".split("(") will split the string into two strings "Ranjit Nayak " and " Attorney )" . Read more about split() method here -docs.oracle.com/javase/7/docs/api/java/lang/…
0

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

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

Comments

0

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());

Comments

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.