JSP中的字符替换函数 str_replace() 实现! 
发布时间:2006-10-14 2:58:20   收集提供:gaoqian
//
// StrReplace.java
//
package forum;
import java.util.*;

/**
* Title:
* Description:
* Copyright: Copyright (c) 2001
* Company:
* @author
* @version 1.0
*/

public class StrReplace {

public StrReplace() {
}
public String str_replace(String from,String to,String source)
{
StringBuffer bf= new StringBuffer("");
StringTokenizer st = new StringTokenizer(source,from,true);
while (st.hasMoreTokens())
{
String tmp = st.nextToken();
System.out.println("*"+tmp);
if(tmp.equals(from))
{
bf.append(to);
}
else
{
bf.append(tmp);
}
}
return bf.toString();
}
}


// 使用方法
<jsp:useBean id="replace" scope="page" class="forum.StrReplace" />
<%= replace.str_replace("<","^","<h1>123456</h1>") %> 
 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50