一道面试题:大家帮忙解决

一道面试题:大家帮忙解决:编写一个函数,输入:字符串和最大字节数,输入按最大字节数截取的字符串,要求,如果字串中有中文不可以截半个,如字串为”我AC中国“ ,最大字节为4,则输出的字串为”我AC“,如果输入的字串为”我AC中国“,最大字节为5,则输出为”我AC中“。
请大家出出主意,这题我没做出来。

byte[stringxxx.length()] baby=stringxxx.getBytes();

byte[max] at string is char or not;
is char----->return new string(byte[0 to max]);
not -------->return new string(byte[0 to max+1]);
哈哈!我没做过,不知道行不行,自己试试

byte[max-1] at string is char or not;

baby[max-1] at string is char or not;

直接用Strring.substring(start,end),中文和字符都当一位,不用区分

test.java
-----------------------------
package test;

public class test {
public test() {
}
public static void main(String[] args) {
String f = "我AC中vc国";//要截取的字符
int l = 8;//要截取的长度
int len = 0;
byte[] f1 = f.getBytes();
if (f1.length < l) {
System.out.println("error");
}
else {
for (int i = 0; i < l; i++) {
if (f1 < 0 && f1[i + 1] < 0) {
len++;
i++;
}
else if (f1 >= 0) {
len++;
}
}
System.out.println(f.substring(0, len));
}
}

}


public static String formatString(String msg, int fixLength)
throws Exception {
byte[] data = msg.getBytes();
if (data.length < fixLength) {
throw new Exception("incorrect para");

}
// 1. get the target
byte[] tmp = new byte[fixLength];
System.arraycopy(data, 0, tmp, 0, fixLength);
data = tmp;
//1.1 prescan
int count = 0;
for (int i = 0; i < data.length; i++) {
if (data[data.length - 1 - i] < 0) {
count++;
}
else {
break;
}

System.out.println(
"char :" + count);
}
//switch the result
switch (count%2) {
case 1 :
{
byte[] tmp2 = new byte[data.length - 1];
System.arraycopy(data, 0, tmp2, 0, data.length - 1);
data = tmp2;
break;
}

}
String reSult=new String(data);
return reSult;
}

呵呵,有点小难度

public static String substring(String getString,int maxLength) throws Exception {
// 此函数是一个字符串的截取函数解决了中文由于双字节截取会出现乱码的问题。思路如下:首先判断截取的长度是否是偶数(因为中文是双字节的)如果不是maxLength+1,然后循环查找被截取的那段字符串中单字节字符的个数是否偶数个如果不是len+1,最后得出的就是被截取的长度len。
String outString ="" ;

byte[] target_byte = getString.getBytes("GBK");
getString = new String(target_byte , "ISO8859_1");

int len = 0;
byte[] byteStr = getString.getBytes("ISO8859_1");
if ( maxLength>=1 ) {
if ( (maxLength % 2) !=0 ) maxLength += 1 ;
if ( getString.length() <= maxLength )
{
outString = getString ;
}
else
{
int enNumber = 0 ;
for (int i = 0; i < maxLength; i++) {
len++;
Byte byteTemp1 = new Byte(byteStr);
if (byteTemp1.intValue() >= 0) enNumber ++ ;
}
if ( (enNumber % 2) !=0 ) len += 1 ;
outString = getString.substring(0, len) + "...";
}
}
else
outString = getString ;
return outString ;
}

--------------------------
如果在你的源码中有加入:
<%@ page language="java" contentType="text/html; charset=GB2312" %>
的话那么直接用String.substring()即可

上一回复的补充:
是加入
< % @ page language="java" contentType="text/html; charset=GB2312" % >
的话直接用String.substring()即可

看看这个行不行,按c的思路作的,可见ASC字符占一个字节且值>=0x20(空格字符),汉字等占两个字节且每个字节值小于0:

public String subChString(String msg, int len)
{
byte[] bMsg = msg.getBytes();

//长度超过字符长直接返回
if(bMsg.length<=len)
return new String(msg);

int end = 0;
for(int i=0;i<len;i++)
{
if(bMsg>0x20) //可见ASC字符
end++;
else if(bMsg<0) //汉字等双字节字符
{
end+=2;
i++;
}
}

return new String(bMsg,0,end);

}

测试代码及结果:
public class TestSubChString
{
public String subChString(String msg, int len)
{
byte[] bMsg = msg.getBytes();

//长度超过字符长直接返回
if(bMsg.length<=len)
return new String(msg);

int end = 0;
for(int i=0;i<len;i++)
{
if(bMsg>0x20) //可见ASC字符
end++;
else if(bMsg<0) //汉字等双字节字符
{
end+=2;
i++;
}
}

return new String(bMsg,0,end);

}

public static void main(String[] args)
{

String aa="我AC中国心123你好!";
int max = aa.getBytes().length;
System.out.println("Stirng aa="+aa+", max bytes len="+max);

TestSubChString tt = new TestSubChString();

for (int i=0;i<max+2 ;i++ )
{
System.out.println("subChString(aa,"+i+")="+tt.subChString(aa,i));
}
}
}
结果:
---------- Run ----------
Stirng aa=我AC中国心123你好!, max bytes len=19
subChString(aa,0)=
subChString(aa,1)=我
subChString(aa,2)=我
subChString(aa,3)=我A
subChString(aa,4)=我AC
subChString(aa,5)=我AC中
subChString(aa,6)=我AC中
subChString(aa,7)=我AC中国
subChString(aa,8)=我AC中国
subChString(aa,9)=我AC中国心
subChString(aa,10)=我AC中国心
subChString(aa,11)=我AC中国心1
subChString(aa,12)=我AC中国心12
subChString(aa,13)=我AC中国心123
subChString(aa,14)=我AC中国心123你
subChString(aa,15)=我AC中国心123你
subChString(aa,16)=我AC中国心123你好
subChString(aa,17)=我AC中国心123你好
subChString(aa,18)=我AC中国心123你好!
subChString(aa,19)=我AC中国心123你好!
subChString(aa,20)=我AC中国心123你好!
Normal Termination
Output completed (0 sec consumed).

String s = "a你好啊abc";

byte ch[]=s.getBytes();
System.out.println(ch.length);
int nOut = 5;
int nLen = 0;
while(nLen <= ch.length && nLen<=nOut){
if ((s.substring(0,nLen).getBytes().length - nOut) >=0)
break;
nLen++;
}
System.out.println(s.substring(0,nLen));

这种问题,没意思,



public static String rimToSize(String str,int size) {
byte[] bts = str.getBytes();
if (bts.length <= size) return str;
size = bts[size - 1] < 0 ? size++:size;
return new String(bts,0,size);
}

++size才是对的

public class FormatString {
String tMsg;
int mLen;
FormatString(String Msg,int Len) {
this.tMsg = Msg;
this.mLen = Len;
}
String subChString() {
byte[] checkMsg = tMsg.getBytes();
int pos = 0;
for(int i=0;i<mLen;) {
if(checkMsg>0x20)
i++;
else if(checkMsg<0) {
i+=2;
}
pos = i;
}

return new String(checkMsg,0,pos);
}
public static void main(String[] args) {
FormatString test = new FormatString("我爱AC中国",7);
System.out.println(test.subChString());
}

}