使用replaceAll 替换中文出现乱码!

浪潮之巅 2009-07-01 02:41:39
如题,下面是代码!


public class TemplateCls
{
public void ReplaceBlock(String s,String inFile)
{
Pattern pattern = Pattern.compile(s);
Matcher matcher = pattern.matcher(Templates);
//替换所有符合正则的数据
Templates = matcher.replaceAll(inFile);
System.out.println(Templates);
}
}


test.jsp

<%@ page language="java" pageEncoding="UTF-8"%>
<%@ page import="szw.TemplateCls;" %>
<%
TemplateCls oTemplate = new TemplateCls();
oTemplate.SetTemplateDir("http://192.168.1.45:8100/szwCMS/Template/");
oTemplate.SetTemplateFile("test.tpl");
String str = "中国";
oTemplate.ReplaceBlock("test",str);
out.write(oTemplate.Parse());
%>
...全文
594 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
树成 2009-07-01
  • 打赏
  • 举报
回复
ISO-8859-1 不能这样直接转换成utf-8格式的。
iso-8859-1 的格式只跟gbk一样。
浪潮之巅 2009-07-01
  • 打赏
  • 举报
回复
是没有问题,是我搞错了,test.jsp文件没有转成UTF-8的。呵呵!
米兔痴愚 2009-07-01
  • 打赏
  • 举报
回复
学习
ZiSheng 2009-07-01
  • 打赏
  • 举报
回复

public class Test{
String templates="testTest";
public void ReplaceBlock(String s,String inFile)
{
Pattern pattern = Pattern.compile(s);
Matcher matcher = pattern.matcher(templates);
//替换所有符合正则的数据
templates = matcher.replaceAll(inFile);
System.out.println(templates);
}
public static void main(String [] args){
new Test().ReplaceBlock("test", "中国");
}
}

没有问题
out:
中国Test
树成 2009-07-01
  • 打赏
  • 举报
回复
ISO-8859-1 不能这样直接转换成utf-8格式的。
iso-8859-1 的格式只跟gbk一样。
lvkai0452 2009-07-01
  • 打赏
  • 举报
回复
new String(iso.getBytes("ISO-8859-1"),"utf-8");

81,091

社区成员

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

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