我写的程序,应该差不多了import java.io.*; import java.util.*;public class Substring{ public static void main(String[] args) throws IOException {
System.out.print("Result:" + getSubString(args[0],Integer.parseInt(args[1]))); } public static String getSubString(String str, int max) { char[] a = str.toCharArray(); int i;
UTF888(UU) 没有考虑中间有汉字的情况jkit(不再灌水)和我的都会产生大量的垃圾gernett21(wolf)写的应该是可以的,但是最后两句 for(int j=0;j<i;j++) aa += a[j]; 太影响效率了。不如用String(char[] value, int offset, int count) 直接构造
呵呵呵呵同意binny所说的 不过应该是用String(byte[] bytes, int offset, int length, String charsetName) 吧。
刚才又想了另一种办法: public static String getSubString(String str, int len) { if (str == null) return ""; if (len <= 0) return "";
byte[] bb = str.getBytes(); if (bb.length < len) { return str; } String result = new String(bb, 0, len); if ("".equals(result)) { result = new String(bb, 0, len+1); } return result; }
截取上次截的byte[]的0 至 length-1(看算法,是向前还是向后,错开一个字节),将
得到的结果new String(byte[]); 结果就是了
你就按照lucumu(沸腾的咖啡) 的方式就可以了
{
if(s == null)
return "";
if (i > s.getBytes().length) {
throw new StringIndexOutOfBoundsException("i > " + s.getBytes().length);
}
int start = i / 2;
int j = 0;
String s1 = s.substring(0, start);
while (s1.getBytes().length < i) {
s1 = s.substring(0, start + j);
j++;
}
return s1;
}可惜StringBuffer没有getBytes()方法,要不就能少产生许多垃圾了
throw new StringIndexOutOfBoundsException("i > " + s.getBytes().length);
改成
throw new StringIndexOutOfBoundsException(i + " > " + s.getBytes().length);
合适一点。TO:sandyen(杉叶)看错题目了吧?呵呵,刚开始我也看错了
int length = str.substring(max-1 , max).getBytes().length;//截取max位置字符的长度;中文则为2
System.out.println("length" + length);
if(length == 1) {
return str.substring(0 , max);
}else{
return str.substring(0 , max-1);
}
}
if (str==null) return "";
if (len<=0) return "";
if (str.getBytes().length < len) {
return str;
}
StringBuffer sb = new StringBuffer(str);
sb.setLength(len);
while(sb.toString().getBytes().length>len) {
sb.deleteCharAt(sb.length() - 1);
} return sb.toString();
}
又学了一招
如果输入的字串为"我AC中国",最大字节为5,则应该输出为"我AC"。"中"不应该出来,如果带上"中",那么最大字节数就成了6了!
import java.util.*;public class Substring{ public static void main(String[] args) throws IOException {
System.out.print("Result:" + getSubString(args[0],Integer.parseInt(args[1])));
} public static String getSubString(String str, int max) {
char[] a = str.toCharArray();
int i;
for(i=0;i<a.length;i++)
{
if(max > 0)
{
if(a[i] > 255)
max--;
max--;
}
else
break;
}
String aa = "";
for(int j=0;j<i;j++)
aa += a[j];
return aa;
}}
for(int j=0;j<i;j++)
aa += a[j];
太影响效率了。不如用String(char[] value, int offset, int count) 直接构造
不过应该是用String(byte[] bytes, int offset, int length, String charsetName) 吧。
if (str == null) return "";
if (len <= 0) return "";
byte[] bb = str.getBytes();
if (bb.length < len) {
return str;
} String result = new String(bb, 0, len);
if ("".equals(result)) {
result = new String(bb, 0, len+1);
} return result;
}
public class StringToBytesTest
{
StringToBytesTest(String strTest,int n){
byte[] b=strTest.getBytes();
if(b[n]<0){
n+=1;
}
System.out.println(new String(b,0,n));
}
public static void main(String[] args)
{
StringToBytesTest bt=new StringToBytesTest("我爱china",3);
}
}
java中的字符(char)是unicode字符,所以都是16位的,英文字母也不例外,
所以根本就不存在能把双字节字符被截断的问题