怎么格式化法,给个DEMO看看。
解决方案 »
- js实现:“打开子页面,父页面被屏蔽(不可用) 变暗”
- 有条件的批量自动输入的问题
- jsp判断问题,大虾快来!在线等,急!
- IE6给DIV设置innerHTML之后DIV自动上移
- 日期正则表达式问题,高手进
- 这样的情况,如果控制图片高宽。
- ▲△▲△-- 挑战性问题,高手请进:如何动态写模态对话框(ModalDialog)的HTML代码
- window.open()方法有几个参数
- 有没有人用过????
- jquery取a里面span的值的问题
- 如何动态改变span区域的样式?比如<span onmouseover="this.runtimeStyle.borderBottomColor='FF0000'">test</span>当鼠标移上时没有变化
- javascript如何蔽屏Alt+F4
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();
} /*
public static String ReplaceIgnoreCase(String source, String oldString, String newString) {
}
*/ /**
* 将字符串格式化成 HTML 代码输出
* 只转换特殊字符,适合于 HTML 中的表单区域
*
* @param str 要格式化的字符串
* @return 格式化后的字符串
*/
public static String toHtmlInput(String str) {
if (str == null) return null; String html = new String(str); html = Replace(html, "&", "&");
html = Replace(html, "<", "<");
html = Replace(html, ">", ">"); return html;
} /**
* 将字符串格式化成 HTML 代码输出
* 除普通特殊字符外,还对空格、制表符和换行进行转换,
* 以将内容格式化输出,
* 适合于 HTML 中的显示输出
*
* @param str 要格式化的字符串
* @return 格式化后的字符串
*/
public static String toHtml(String str) {
if (str == null) return null; String html = new String(str); html = toHtmlInput(html);
html = Replace(html, "\r\n", "\n");
html = Replace(html, "\n", "<br>\n");
html = Replace(html, "\t", " ");
html = Replace(html, " ", " "); return html;
}
function change(text)
{
var r, re; // 声明变量。
re = /(\n)/g; // 创建正则表达式模式。
r = text.replace(re, "<br>"); // 交换每一对单词。
re=/(java)/g;
r=r.replace(re,"<b>j</b>ava");
return(r);
}
解决了,谢谢大家。