哪有你这么读的阿!当然如果你非要这么读的话,先把编码变为UTF-8就可以了前面3个字母每个字母一个字节!
解决方案 »
- Java 添加的按钮运行后不显示,调整大小后才会出来?
- IO相关问题 BufferedWriter对象 什么时候要调用flush 什么时候不用调用
- 过时的API问题
- SQL Server2000的查询分析器怎么不见了?
- 怎么在""中包含//
- 高手接分题
- 一个诡异的字符串问题
- 如何读取dbf文件中已被删除的数据?(100分了)
- 菜鸟求助:用什么办法替换 public boolean action(Event e,Object o) 和 public boolean handleEvent(Event e)?
- 各位老哥帮帮忙,这个问题。。。呵,呵,我不懂!
- 如何在Jbuilder中使用类的继承?
- 关于java.io.Serializable的问题
明明只读了“abc”三个字母阿!
public static void main(String[] args){
String test = "abc中国";
byte[] byteArray = test.getBytes();
// String newTest = new String(byteArray, 0, 4); 没有任何输出
// String newTest = new String(byteArray, 0, 3); 输出 abc
String newTest = new String(byteArray, 0, 5); 输出 abc中
System.out.println(newTest);
}
}
输出6字节才会是“abc中”
好象都不行吗
//import java.util.*;
//import java.text.*;
import java.io.*;
public class InterceptChString
{
public String subChString(String msg, int nOut){ byte ch[]=msg.getBytes();
if(ch.length<=nOut) return msg;
int nLen = 0;
while(nLen <= ch.length && nLen<=nOut){
if ((msg.substring(0,nLen).getBytes().length - nOut) >=0)
break;
nLen++;
}
return(msg.substring(0,nLen));
}
}
public static void main(String[] args) {
String s = "abc中国";
byte[] b = s.getBytes();
StringBuffer buf = new StringBuffer(4);
for(int i=0;i<4;i++) {
char c = (char)b[i];
buf.append(c);
}
System.out.println(buf);
}
}
效果不好 :-(,据说很多可以删除半个汉字的编辑软件之所以出现乱码是因为和前面或者后面的字符进行了整合。