public class test2 {
/**
* 10、 编写一个截取字符串的函数,输入为一个字符串和字节数,输出为按字节截取的字符串。
* 但是要保证汉字不被截半个,如“我ABC”4,应该截为“我AB”,输入“我ABC汉DEF”,6,应该输出为“我ABC”而不是“我ABC+汉的半个”。
*
*/
public static void main(String[] args) {
String ss=”我AbcS你好”;
subSs(ss,4);
}
public static void subSs(String src,int len){
if(src==null){
System.out.println(“输入的字符串为空”);
}
int size=src.length();
byte[] by=src.getBytes();
if(len>size){
len=size;
}
if(by[len]<0){
String sub=new String(by,0,–len);
System.out.println(sub);
}else{
String sub=new String(by,0,len);
System.out.println(sub);
}
}
}
编写一个截取字符串的函数,输入为一个字符串和字节数,输出为按字节截取的字符串。 * 但是要保证汉字不被截半个,如“我ABC”4,应该截为“我AB”,输入“我ABC汉DEF”,6,应该输出为“我A
最新推荐文章于 2019-06-13 20:23:29 发布