我在这里工作了一段时间,在这里没有发现任何关于此的内容,所以我想我会发布我的解决方案来批评/有用。从字节数组中删除多余的“空白”字符并转换为字符串
import java.lang.*;
public class Concat
{
public static void main(String[] args)
{
byte[] buf = new byte[256];
int lastGoodChar=0;
//fill it up for example only
byte[] fillbuf=(new String("hello").getBytes());
for(int i=0;i
buf[i]=fillbuf[i];
//Now remove extra bytes from "buf"
for(int i=0;i
{
int bint = new Byte(buf[i]).intValue();
if(bint == 0)
{
lastGoodChar = i;
break;
}
}
String bufString = new String(buf,0,lastGoodChar);
//Prove that it has been concatenated, 0 if exact match
System.out.println(bufString.compareTo("hello"));
}
}
+0
我打电话这不是一个真正的问题,因为你张贴在这个问题的解决方案,基本要求的答案是问题/批评。如果您想探索做某事的最佳方式,请将需求作为问题妥善定义,然后针对您自己的问题发布答案。这样我们可以上/下投票或在评论中提出建议。 –
2010-10-04 18:00:50
+0
对,对不起。这并不意味着要像分享代码一样提出问题。我发现通常会导致更多建设性的批评。而且,作为一个非CS的人,当你不知道如何沟通你想做的事时,会发现它有帮助。 –
2010-10-04 18:31:11