Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove brackets [] from a list set to a textview?

Tags:

java

android

I am parsing content using the following code with jsoup.

  try{
 Elements divElements = jsDoc.getElementsByTag("div");
 for(Element divElement : divElements){
     if(divElement.attr("class").equals("article-content")){
         textList.add(divElement.text());
         text = textList.toString();
     }
 }
}
catch(Exception e){

System.out.println("Couldnt get content");
       }

The only problem is the content is returned with brackets around it [] like that.

Im guessing it is becaue of the list i am setting it to. How can i remove these?

like image 916
android_king22 Avatar asked Sep 24 '11 00:09

android_king22


People also ask

How do you remove square brackets from string?

Brackets can be removed from a string in Javascript by using a regular expression in combination with the . replace() method.


2 Answers

Using regex to replace the leading and trailing brackets, String.replace() doesn't work for the edge cases that the list's content contains brackets.

String text = textList.toString().replaceAll("(^\\[|\\]$)", "");
like image 65
Sapience Avatar answered Oct 19 '22 16:10

Sapience


Replace:

text = textList.toString();

with:

text = textList.toString().replace("[", "").replace("]", "");
like image 22
Eng.Fouad Avatar answered Oct 19 '22 18:10

Eng.Fouad