需要获取多行字符串以在文本框中显示Java
我的项目中有一个需求。 我在 JavaScript 中生成一个注释字符串。
Coping Option: Delete all codes and replace
Source Proj Num: R21AR058864-02
Source PI Last Name: SZALAI
Appl ID: 7924675; File Year: 7924675
我将其发送到服务器,在服务器中将其作为字符串存储在数据库中,然后将其检索回来并将其显示在文本区域中。
我在 javascript 中生成它:
codingHistoryComment += 'Source Proj Num: <%=mDefault.getFullProjectNumber()%>'+'\n';
codingHistoryComment += 'Source PI Last Name: <%=mDefault.getPILastName()%>'+'\n';
codingHistoryComment += 'Appl ID: <%=mDefault.getApplId()%>; File Year: <%=mDefault.getApplId()%>'+'\n';
在 java 中,我试图将 \n 替换为
:
String str = soChild2.getChild("codingHistoryComment").getValue().trim();
if(str.contains("\\n")){
str = str.replaceAll("(\r\n|\n)", "<br>");
}
但是文本区域仍然填充有“\n”字符:
Coping Option: Delete all codes and replace\nSource Proj Num: R21AR058864-02\nSource PI Last Name: SZALAI\nAppl ID: 7924675; File Year: 7924675\n
谢谢。
I have a requirement in my project.
I generate a comment string in javascript.
Coping Option: Delete all codes and replace
Source Proj Num: R21AR058864-02
Source PI Last Name: SZALAI
Appl ID: 7924675; File Year: 7924675
I send this to server where I store it as a string in db and then after that I retrieve it back and show it in a textarea.
I generate it in javascript as :
codingHistoryComment += 'Source Proj Num: <%=mDefault.getFullProjectNumber()%>'+'\n';
codingHistoryComment += 'Source PI Last Name: <%=mDefault.getPILastName()%>'+'\n';
codingHistoryComment += 'Appl ID: <%=mDefault.getApplId()%>; File Year: <%=mDefault.getApplId()%>'+'\n';
In java I am trying to replace the \n to
:
String str = soChild2.getChild("codingHistoryComment").getValue().trim();
if(str.contains("\\n")){
str = str.replaceAll("(\r\n|\n)", "<br>");
}
However the textarea still get populated with the "\n" characters:
Coping Option: Delete all codes and replace\nSource Proj Num: R21AR058864-02\nSource PI Last Name: SZALAI\nAppl ID: 7924675; File Year: 7924675\n
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
不要替换“\n”。 JTextArea 会将其解析为新行字符串。
尝试将其转换为“br”标记也无济于事,因为 JTextArea 不支持 html。
我总是使用如下代码来用文本填充文本区域:
Don't replace the "\n". A JTextArea will parse that as a new line string.
Trying to convert it to a "br" tag won't help either since a JTextArea does not support html.
I always just use code like the following to populate a text area with text:
来自此处。
From here.
这是一个有效的测试,请尝试一下:
Here is a test that works, try it out:
假设 Javascript(因为您尝试用 HTML 换行符替换):
HTML 文本区域换行符应该是换行符
\n
而不是 HTML 换行符
。尝试使用下面的代码来删除多余的斜杠,而不是当前的 if 语句并替换。替换后不要忘记将值分配给文本区域。尝试:
Assuming Javascript (since you try to replace with a HTML break line):
A HTML textarea newline should be a newline character
\n
and not the HTML break line<br>
. Try to use the code below to remove extra slashes instead of your current if statement and replace. Don't forget to assign the value to the textarea after the replacement.Try:
我认为你的问题在这里:
而不是
"\\n"
你只需要"\n"
然后而不是
"\n"
你这里需要"\\n"
:顺便说一下,实际上并不需要
if(str.contains()
,因为如果有的话运行replace all不会有什么坏处没有"\n"
字符。I think your problem is here:
Instead of
"\\n"
you just need"\n"
Then instead of
"\n"
you need"\\n"
here:By the way, the
if(str.contains()
is not really needed because it won't hurt to run replace all if there is no"\n"
characters.