package cn3;
/** </p>
* 作者: </p>
* 邮箱: </p>
* 创建时间:</p>
* 类说明: 编写一个截取字符串的函数</p>
* 修改记录: </p>
*/
public class DealWithString {
public static void main(String[] args) throws Exception {
String str = "我a爱中华abc我爱传智def";
// String str = "我ABC汉";
System.out.println(str.getBytes("GBK"));
int num = trimGBK(str.getBytes("GBK"), 5);
System.out.println(str.substring(0, num));
}
public static int trimGBK(byte[] buf, int n) {
int num = 0;
boolean bChineseFirstHalf = false;
for (int i = 0; i < n; i++) {
if (buf[i] < 0 && !bChineseFirstHalf) {
bChineseFirstHalf = true;
} else {
num++;
bChineseFirstHalf = false;
}
}
return num;
}
}
编写一个截取字符串(含中英文数字)
最新推荐文章于 2022-05-05 22:56:40 发布