-1

My string when printed looks like:

line1
line2
line3

as it's a extracted with selenium and it looks in html:

line1
<br>
line2
<br>
line3
<br>

I want to transform that string to list of 3 elements, i tried the following:

List<String> test= new ArrayList<String>(Arrays.asList(mystring.split("<br>")));

but it did not work i got a list of 1 element as follows:

[line1
line2
line3]
DebanjanB
  • 118,661
  • 30
  • 168
  • 217
Steve
  • 27
  • 4

3 Answers3

1
Arrays.asList

already returns the list, no need to wrap it once again.

List<String> test= Arrays.asList(mystring.split("<br>"));
Backflip
  • 235
  • 3
  • 8
  • that did not work, i printed : System.out.println(Arrays.toString(test.toArray())); and got the same result – Steve Jul 26 '20 at 09:57
0

you can use Arrays.asList directly as follow

List<String> result = Arrays.asList(myString.split("<br>"));

But the List we get has a limit,this list is Unmodifiable, if you want to do

result.add("element");

you will get an UnsupportedOperationException

if you want to modify your list as follow,you can get it such as

String[] split = myString.split("<br>");
List<String> result = Arrays.stream(split).collect(Collectors.toList());

you can get a modifiable list.

LuckyCurve
  • 23
  • 5
0

To split() the text by <br> and place into a you need to induce WebDriverWait for the visibilityOfElementLocated() and you can use either of the following Locator Strategies:

  • Using cssSelector and split():

    String[] cssParts = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("parentElementCssSelector"))).getText().split("<br>");
    System.out.println(cssParts);
    
  • Using xpath and split():

    String[] xpathParts = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("parentElementXpath"))).getText().split("<br>");
    System.out.println(xpathParts);
    

References

You can find a couple of relevant discussions in:

DebanjanB
  • 118,661
  • 30
  • 168
  • 217