Extra line breaks at end of text

爷,独闯天下 提交于 2019-12-11 10:56:36

问题


I seem to be getting what seems like some extra line breaks after using this method to set the text of a TextView

message.setText(Html.fromHtml( message ));

How can I remove these? They cause my layout to get warped since it adds two extra lines to the output.

The string was saved to my sqlite database via Html.toHtml( editText.getText() ).trim();

Initial string input : hello

Log output of the message variable: <p dir="ltr">hello</p>


回答1:


Looks like toHtml assumes everything should be in a <p> tag. I'd strip off the beginning and ending <p> and </p> tags before writing to the database.




回答2:


you can use this lines ... totally works ;)

i know your problem solved but maybe some one find this useful .

 try{
        string= replceLast(string,"<p dir=\"ltr\">", "");
        string=replceLast(string,"</p>", "");
}catch (Exception e) {}

and here is replaceLast ...

public String replceLast(String yourString, String frist,String second)
{
    StringBuilder b = new StringBuilder(yourString);
    b.replace(yourString.lastIndexOf(frist), yourString.lastIndexOf(frist)+frist.length(),second );
    return b.toString();
}



回答3:


For kotlin you can use

html.trim('\n')


来源:https://stackoverflow.com/questions/22418765/extra-line-breaks-at-end-of-text

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!