用textarea获取数据后,各种特殊字符怎么转换?

zy12345 2001-05-22 12:18:00
用textarea获取数据后,各种特殊字符怎么转换。
的是mysql数据库。
比如空格,单引号,\.等等。

最头疼的是如果获取的是html语言字符,如<br>、<html>
等等,无法显示出来?

请问以上问题怎么解决,万分感谢!
...全文
848 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
asper 2001-05-24
  • 打赏
  • 举报
回复
有个写好的bean
/*Format.java*/

public class Format {

/**
* 字符串替换,将 source 中的 oldString 全部换成 newString
*
* @param source 源字符串
* @param oldString 老的字符串
* @param newString 新的字符串
* @return 替换后的字符串
*/
public static String Replace(String source, String oldString, String newString) {
StringBuffer output = new StringBuffer();

int lengthOfSource = source.length(); // 源字符串长度
int lengthOfOld = oldString.length(); // 老字符串长度

int posStart = 0; // 开始搜索位置
int pos; // 搜索到老字符串的位置

while ((pos = source.indexOf(oldString, posStart)) >= 0) {
output.append(source.substring(posStart, pos));

output.append(newString);
posStart = pos + lengthOfOld;
}

if (posStart < lengthOfSource) {
output.append(source.substring(posStart));
}

return output.toString();
}

/**
* 将字符串格式化成 HTML 代码输出
*
* @param str 要格式化的字符串
* @return 格式化后的字符串
*/
public static String toHtml(String str) {
String html = str;

html = Replace(html, "&", "&");
html = Replace(html, "<", "<");
html = Replace(html, ">", ">");
html = Replace(html, "\r\n", "\n");
html = Replace(html, "\n", "<br>\n");
html = Replace(html, "\t", " ");
html = Replace(html, " ", "  ");

return html;
}

/**
* 把单引号替换为双单引号,用于MSsql
*/

public static String toSql(String sqlstr) {
String strsql = sqlstr;

strsql = Replace(strsql, "'", "''");
return strsql;
}

}
zy12345 2001-05-24
  • 打赏
  • 举报
回复
为什么没人回答??高手呢?
asper 2001-05-24
  • 打赏
  • 举报
回复
Replace(strsql,"\",其他字符)
zy12345 2001-05-24
  • 打赏
  • 举报
回复
\

81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧