字符串截取(按指定的字节数)汉字不可以截取半个
2012-09-22 20:47
316 查看
package com.interview.demo;
/**
* 截取字符串 按指定的字节数
* 汉子不可截取半个 abc中国人 4 输出:abc 而不是abc+半个人
*/
import java.io.UnsupportedEncodingException;
public class WhiltCat{
public static boolean isChinese(char ch) throws UnsupportedEncodingException{
return String.valueOf(ch).getBytes("GBK").length>1;
}
public static String cutString(String str,int count) throws UnsupportedEncodingException{
if(str!=null&&!"".equals(str)){
str = new String(str.getBytes(),"GBK");
if(count>0&&count<str.getBytes("GBK").length){
StringBuffer buf = new StringBuffer();
char c;
int num = 0;
for(int i=0;i<str.length()-1;i++){
c = str.charAt(i);
//从头扫描每个字符 首先判断是不是汉子 接着判断当前截取的字节数+2是否超过了要截取的字节数,
//如果超过舍弃截取此汉字;否则加入;
//
if(isChinese(c)){
if(num+2<=count){
buf.append(c);
num = num+2;
}else{
break;
}
}else{
if(num+1<=count){
buf.append(c);
num=num+1;
}else{
break;
}
}
}
return buf.toString();
}
}
return str;
}
public static void main(String[] args){
String str = "到遇到jfr是中hinese";
try {
System.out.println(cutString(str,7));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}
/**
* 截取字符串 按指定的字节数
* 汉子不可截取半个 abc中国人 4 输出:abc 而不是abc+半个人
*/
import java.io.UnsupportedEncodingException;
public class WhiltCat{
public static boolean isChinese(char ch) throws UnsupportedEncodingException{
return String.valueOf(ch).getBytes("GBK").length>1;
}
public static String cutString(String str,int count) throws UnsupportedEncodingException{
if(str!=null&&!"".equals(str)){
str = new String(str.getBytes(),"GBK");
if(count>0&&count<str.getBytes("GBK").length){
StringBuffer buf = new StringBuffer();
char c;
int num = 0;
for(int i=0;i<str.length()-1;i++){
c = str.charAt(i);
//从头扫描每个字符 首先判断是不是汉子 接着判断当前截取的字节数+2是否超过了要截取的字节数,
//如果超过舍弃截取此汉字;否则加入;
//
if(isChinese(c)){
if(num+2<=count){
buf.append(c);
num = num+2;
}else{
break;
}
}else{
if(num+1<=count){
buf.append(c);
num=num+1;
}else{
break;
}
}
}
return buf.toString();
}
}
return str;
}
public static void main(String[] args){
String str = "到遇到jfr是中hinese";
try {
System.out.println(cutString(str,7));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}
相关文章推荐
- 面试题之——对包含汉字的字符串截取指定字节数
- 从一个包含汉字和其他字符的字符串中截取指定字节长度的字符串,不可以出现中文乱码
- Java根据字节数截取字符串,但是汉字不能截取半个。
- 截取指定长度的字符串(汉字不出现半个)
- 截取字符串指定指定字节数的内容,汉字不能截取部分
- Java:根据字节数截取字符串,但是汉字不能截取半个。
- 从一个字符串中按字节数截取一部分,但不能截取出半个中文(GBK码表)【笔记】
- Java中文字符串截取,不能返回半个汉字
- Java按照字节来截取字符串的工具,不会出现半个汉字
- 编写一个截取字符串的函数,输入为一个字符串和字节数,输出为按字节截取的字符串。 但是要保证汉字不被截半个,如“我ABC”4,应该截为“我AB”,输入“我ABC汉DEF”,6,应该输出为“我ABC”而不是“我ABC+汉的半个”。
- 编写一个截取字符串的函数,(网上流传的答案有的是错的)输入为一个字符串和字节数,输出为按字节截取的字符串。 但是要保证汉字不被截半个,如“我ABC”4
- 有汉字字符串的截取,汉字不能截半个
- 截取一个中英文混合的字符串,不能出现截取半个汉字的问题
- 编写函数,从一个字符串中按字节数截取一部分,但不能截取出半个中文(GBK码表),两种方法的比较。
- JAVA 中字符串的截取,要求不能截取半个汉字
- 编写一个截取字符串的函数,输入为一个字符串和字节数,输出为按字节截取的字符串。 但是要保证汉字不被截半个,如“我ABC”4,应该截为“我AB”,输入“我ABC汉DEF”,6,应该输出为“我ABC”而不是“我ABC+汉的半个”。
- 编一个截取字符串的函数,输入为一个字符串和字节数,输出为按字节截取的字符串,要保证汉字不被截取半个,如“我ABC”,4,应该截取“我AB”,输入“我ABC汉DEF”,6,应该输出“我ABC”,
- 按照字节数截取一个字符串,“abc你好” 如果截取到半个中文,舍弃,比如 截取4个字节 abc。截取 5个字节abc你
- 截取字符串,但要保证汉字不被截取半个
- C# 根据字节数截取字符串(区别汉字,英文,数字)