换行符\ n不起作用,它显示为字符串 [英] Line break \n doesn't work, it show as string

查看:3673
本文介绍了换行符\ n不起作用,它显示为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有(\ n)作为换行符的字符串

I have a String with (\n) as a line break

String mytext = "<p>Your email is not correct.\nPlease check again</p>"
//(mytext value load from my database)

我像这样将mytext放在webview上:

And i put mytext on webview like this :

web_view.loadDataWithBaseURL("", mytext, "text/html", "UTF-8", "");

但是,我不知道为什么\ n不能作为换行符,而是显示为字符串"\ n"?

But, i don't know why \n doesn't work as line break, but it show as String "\n"?

如何解决?

我尝试用mytext.replace("\n","</ br>")mytext.replaceAll("\n", "</ br>")替换为</ br>.

谢谢

已更新

解决方法是:

mytext.replace ("\\r\\n", "<br>").replace ("\\n", "<br>");

谢谢@ChristianJonassen的帮助.

Thank you, @ChristianJonassen for your help.

推荐答案

您正在使用HTML,需要引入HTML换行符.

You are using HTML, you need to introduce an HTML line break.

在您的情况下,页面的源代码将使用换行符,而不是用户的表示形式.

In your case, the source code of the page will be given the line break, but not the representation given to the user.

以下是解决方法:

String mytext = "<p>Your email is not correct.<br />Please check again</p>"

由于它来自外部服务,因此您可以通过替换\n来编辑字符串:

Since it comes from an external service, you can edit the String by replacing \n:

mytext.replaceAll("\n", "<br />");

这篇关于换行符\ n不起作用,它显示为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆