急:请教一个中文字符串匹配问题

oDon 2007-10-12 04:56:38

InputStream is = getClass().getResourceAsStream("/BookMark/" + this.MARK_PATH);
if (is != null) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int ch = 0;
try {
while ((ch = is.read()) != -1) {
baos.write(ch);
}
} catch (IOException e) {
e.printStackTrace();
}
byte[] text = baos.toByteArray();
try {
baos.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
str = new String(text, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}

现在用UTF-8读取了.txt文件中的内容存在buffer中,现在用一个中文字符串与之匹配(此中文字符串是用FileConnection读取的文件夹下的文件名称)与buffer中匹配,只有名称为ascii码才能匹配(indexOf返回正确),否则如果是中文匹配就返回-1.

public int getBookMark(String name) {
int st = 0;
int ed = 0;
String[] array = null;
System.out.println(name);
st = bookMarks.indexOf(name);
if(st == -1)
return -1;
ed = bookMarks.indexOf(";", st);
array = Config.split(bookMarks.substring(st, ed), "=");
return Integer.parseInt(array[1]);
}

请问如何正确转换字符串使之匹配。
很是头疼,希望达人不吝赐教。
...全文
221 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
liuzi123 2007-10-19
  • 打赏
  • 举报
回复
关注下
xiyuan1999 2007-10-13
  • 打赏
  • 举报
回复
呵呵

顶上去

那不也一样的
oDon 2007-10-13
  • 打赏
  • 举报
回复
现在改了思路,采用HashCode存储了,实现功能。谢谢各位。
aptweasel 2007-10-12
  • 打赏
  • 举报
回复
我不知道别人是怎么用的,一般为了系统的通用性,一般我们只能收到的数据进行转码,并不指定到底转成什么类型的字符,
如String st=new String("*********");
String s2=new String(st.getByte("utf-8")),这样他生成的数据是以操作系统的字符集来决定,因我一般我们生成的字符类型,查看好像正确,但是数据保存到数据库里又变成乱码了,如果以操作系统为标准的话,一般就不会出问题了。

当然如果你知道自己的应用环境的话,不会出现别的问题,指定字符类型的话,当然最好了。
qybao 2007-10-12
  • 打赏
  • 举报
回复
have a try
name = new String(name.getBytes("UTF-8"), "UTF-8"); // add code here to have a try
st = bookMarks.indexOf(name);
if(st == -1)
return -1;
ed = bookMarks.indexOf(";", st);
array = Config.split(bookMarks.substring(st, ed), "=");
陈宝峰 2007-10-12
  • 打赏
  • 举报
回复
因为你从文件读入的字符串缓存在buffer中,字符串编码是utf-8,那么你在参数中传入的name中的字符串内容也要用utf-8编码才能得出正确的比较结果,否则肯定是不正确的。

62,623

社区成员

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

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