黑马程序员技术交流社区
标题:
分享一个面试题
[打印本页]
作者:
蔡锐
时间:
2015-7-26 08:04
标题:
分享一个面试题
package com.itheima;
import java.io.UnsupportedEncodingException;
import java.util.Scanner;
/*
* 10、编写函数,从一个字符串中按字节数截取一部分,但不能截取出半个中文
* (GBK码表),例如:从“HM程序员”中截取2个字节是“HM”,截取4个则是“HM程”,
* 截取3个字节也要是"HM"而不要出现半个中文
*
* 思路:
* 将字符串转换成字节数组存储
* 对要进行截取的部分统计负值个数
* 如果负数个数为单数,那么为半个中文,舍弃,返回-1之前的数,转换成字符串输出
* 否则为一个中文,转换成字符串输出
*/
public class Test10 {
public static void main(String[] args) throws UnsupportedEncodingException {
// while (true) {
Scanner sc = new Scanner(System.in);
System.out.print("请输入字符串:");
String str = sc.nextLine();
System.out.println("请输入要截取的字节数:");
int len = sc.nextInt();
System.out.println(subStr(str, len));
// }
}
private static String subStr(String str, int len) throws UnsupportedEncodingException {
if (str == null && "".equals(str)) {
return null;
}
// 将字符串中的char数组转换成指定编码方式的byte数组的函数
byte[] strBytes = null;
strBytes = str.getBytes("GBK");
// 得到字符串的长度,判断截取字符串的长度是否在判断的范围内,否则返回原串
if (len >= strBytes.length || len < 1) {
return str;
}
int count = 0;
for (int i = 0; i < len; i++) {
// 将每个字节数组转换为整型数,以为后面根据值的正负来判断是否为汉字
int value = strBytes[i];
// 如果是汉字(负),则统计截取字符串中的汉字所占字节数
if (value < 0)
count++;
}
if(count%2==0)//解码。如果为单数就是半个中文,要舍弃最后的数
return new String(strBytes,0,len,"GBK");
else
return new String(strBytes,0,len-1,"GBK");
}
}
复制代码
作者:
蔡锐
时间:
2015-7-26 08:06
准确说是基础测试题
作者:
libin
时间:
2015-7-26 08:17
原来中文转换成字节形式都是负数,而且是两字节表示,受教了。
作者:
小小豪
时间:
2015-7-26 12:18
原来如此。
作者:
zhaoyue
时间:
2015-7-26 13:13
。。感谢大神。。我也查到用法。。
作者:
pengbin
时间:
2015-7-26 13:23
感谢大神的分享
作者:
田利阳
时间:
2015-7-26 13:41
感谢分享
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2