今天下午南通 江苏中威 的机试题(CoreJava) 1 编写一个截取字符串的函数,输入为一个字符串的字节数,输出为按字节截取的字符串。 但是要保证汉字不被截半个,如“我ABC” 4 , 应该截为“我AB”, 输入“我ABC汉DEF”6 ,应该输出为“我ABC”,而不是“我ABC+汉的半个”。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 package january;public class Test { public static void main(String[] args) { String str="我ABC汉DEF"; int n=6; int byteLength=0; byte[] b=str.getBytes(); System.out.println(b.length); //调整n for(int i=0;i<=b.length;i++){ if((str.charAt(i)+"").getBytes().length==1){//不是汉字 byteLength+=1; System.out.println("不是汉字"); } else{ byteLength+=2; System.out.println("汉字"); } if(byteLength>n){ byteLength-=2; break ; } if(byteLength==n){ break ; } } byte[] bb=new byte[byteLength]; for(int i=0;i<byteLength;i++){ bb[i]=b[i]; } System.out.println(new String(bb)); }} 上面那些没有优化,n就是 要截取的长度..str就是原始的, 最后输出的 就是 结果了. 这种垃圾公司出的垃圾题目,一目了然不过是Java 里面char也是16位的 首先要了解中文字符有多种编码及各种编码的特征。 假设n为要截取的字节数。 public static void main(String[] args) throws Exception{ String str = "我a爱中华abc我爱传智def'; String str = "我ABC汉"; 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; } public static void split(String source,int num) throws Exception { int k=0; String temp=""; for (int i = 0; i <source.length(); i++) { byte[] b=(source.charAt(i)+"").getBytes(); k=k+b.length; if(k>num) { break; } temp=temp+source.charAt(i); } System.out.println(temp); } public static void main(String[] args) { String str = "我ABC汉DEF"; System.out.println(subString(str, 6)); } public static String subString(String str, int length){ char[] chars = str.toCharArray(); int temp = 0; int index = 0; while(temp <= length){ if(index >= chars.length) return str; if(chars[index++] > 255) temp += 2; else temp += 1; } return str.substring(0, index - 1); } class SplitString { public static String split(String str,int num) { byte[] strs = str.getBytes(); if(strs[num-1]<0) { num=num-1; } byte[] news = new byte[num]; System.arraycopy(strs,0,news,0,num); return new String(news); } public static void main(String[] args) { String str = split("我ABC", 4); System.out.println(str); String str2 = split("我ABC走DEF", 6); System.out.println(str2); } } public static String split(String str, int num) { byte[] strs = str.getBytes(); if (strs[num - 1] < 0) { num = num - 1; } byte[] news = new byte[num]; System.arraycopy(strs, 0, news, 0, num); return new String(news); } public static void main(String[] args) { String str = split("我ABC", 4); System.out.println(str); String str2 = split("我ABC走DEF", 6); System.out.println(str2); } 为啥我这个线程问题乱码? 有关JDK,JVM和JRE的问题 spring-framework-reference.pdf--看完结贴! 关于值在两个方法间的传递 听说有很框架,我还是搞不清? 怎样在JAVA应用程序中设置类路径 请问要想知道访问者在我的页面上停留的时间,用什么方法实现? postgresql的jdbc不 能使用请问那位清楚谢谢. oracle的jdbc的配置 高分请教!每人给5分先! 请教Timer定时器的用法 java单继承的优点到底是什么?接口真的可以掩盖多继承的“缺点”吗?
package january;public class Test {
public static void main(String[] args) {
String str="我ABC汉DEF";
int n=6;
int byteLength=0;
byte[] b=str.getBytes();
System.out.println(b.length);
//调整n
for(int i=0;i<=b.length;i++){
if((str.charAt(i)+"").getBytes().length==1){//不是汉字
byteLength+=1;
System.out.println("不是汉字");
}
else{
byteLength+=2;
System.out.println("汉字");
}
if(byteLength>n){
byteLength-=2;
break ;
}
if(byteLength==n){
break ;
}
}
byte[] bb=new byte[byteLength];
for(int i=0;i<byteLength;i++){
bb[i]=b[i];
}
System.out.println(new String(bb));
}
}
不过是Java 里面char也是16位的
假设n为要截取的字节数。
public static void main(String[] args) throws Exception{
String str = "我a爱中华abc我爱传智def';
String str = "我ABC汉";
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;
}
public static void split(String source,int num) throws Exception
{
int k=0;
String temp="";
for (int i = 0; i <source.length(); i++)
{
byte[] b=(source.charAt(i)+"").getBytes();
k=k+b.length;
if(k>num)
{
break;
}
temp=temp+source.charAt(i);
}
System.out.println(temp);
}
public static void main(String[] args) {
String str = "我ABC汉DEF";
System.out.println(subString(str, 6));
} public static String subString(String str, int length){
char[] chars = str.toCharArray();
int temp = 0;
int index = 0;
while(temp <= length){
if(index >= chars.length)
return str;
if(chars[index++] > 255)
temp += 2;
else
temp += 1;
}
return str.substring(0, index - 1);
}
class SplitString { public static String split(String str,int num) { byte[] strs = str.getBytes(); if(strs[num-1]<0) { num=num-1; } byte[] news = new byte[num]; System.arraycopy(strs,0,news,0,num); return new String(news); } public static void main(String[] args) { String str = split("我ABC", 4); System.out.println(str); String str2 = split("我ABC走DEF", 6); System.out.println(str2); } }
public static String split(String str, int num) {
byte[] strs = str.getBytes();
if (strs[num - 1] < 0) {
num = num - 1;
}
byte[] news = new byte[num];
System.arraycopy(strs, 0, news, 0, num);
return new String(news);
} public static void main(String[] args) {
String str = split("我ABC", 4);
System.out.println(str);
String str2 = split("我ABC走DEF", 6);
System.out.println(str2);
}