■式样要求如下
要求从左向右取5个文字(全角,半角都可能包含)。
假如:
String s1= "测试数据从左向右取得";
String s2= "0测试数据从左向右取得";
String s3= "0123456789"; 结果:
S1的场合:"测试"
S2的场合:"0测试"
S3的场合:"01234" 注意:S1的场合,因为"数"占有2个字节,不能将一个字从中间断开,所以只取前面的数据。■实现方式
我用了getBytes()等方法,搞了一天也没实现。请高手帮忙阿,最好能写个通过的方法。
要求从左向右取5个文字(全角,半角都可能包含)。
假如:
String s1= "测试数据从左向右取得";
String s2= "0测试数据从左向右取得";
String s3= "0123456789"; 结果:
S1的场合:"测试"
S2的场合:"0测试"
S3的场合:"01234" 注意:S1的场合,因为"数"占有2个字节,不能将一个字从中间断开,所以只取前面的数据。■实现方式
我用了getBytes()等方法,搞了一天也没实现。请高手帮忙阿,最好能写个通过的方法。
那你还需要转啦
怎么这么个需求。楼主还是讲明白点。
String 一般截取字符串 用subString(),就行了。
getBytes() 是使用平台默认的字符集,汉字2字节,字母和字符1字节。public class Test {
public static void main(String[] args) {int beginFlag = 0;
int endFlag = 5;
StringBuffer outStr = new StringBuffer();
String codingStr = "java程序";
for (Character i : codingStr.toCharArray()) {
int length = i.toString().getBytes().length;
if (beginFlag < endFlag) {
beginFlag += length;
outStr.append(i.toString());
}
}
System.out.println(outStr.toString());
}
}
跑一下就知道了
不可以一个字符一个字符的录,判断全半然后用counter累加吗?
我是新手,刚学一个星期,54我……
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.util.Arrays;public class Decoder { public static String decode(byte[] b, String charsetName) {
ByteBuffer in = ByteBuffer.wrap(b);
Charset charset = Charset.forName(charsetName);
CharsetDecoder decoder = charset.newDecoder();
CharBuffer out = CharBuffer.allocate(b.length);
out.clear();
decoder.decode(in, out, false);
out.flip();
return out.toString(); } public static void main(String[] args) throws IOException {
String s = "abc测c试字符串试试看";
byte[] b = s.getBytes("UTF-8");
byte[] r = Arrays.copyOf(b, 9);
System.out.println(decode(r, "UTF-8")); }}
public static String leftByBytes(String input, int length, String encoding){
if (input==null) return null;
if (length<=0) return "";
StringBuffer result= new StringBuffer("");
int byteLength = 0;
int index = 0;
while (index < input.length()){
String current = input.substring(index, index+1);
int wide;
try {
wide = current.getBytes(encoding).length;
} catch (UnsupportedEncodingException e) {
wide = current.getBytes().length;
}
if (byteLength + wide > length) break;
result.append(current);
byteLength += wide;
index ++;
}
return result.toString();
}
/**
* @param args
*/
public static void main(String[] args) {
String s1= "测试数据从左向右取得";
String s2= "0测试数据从左向右取得";
String s3= "0123456789";
String s4= "短";
System.out.println(leftByBytes(s1, 5, "GBK"));
System.out.println(leftByBytes(s2, 5, "GBK"));
System.out.println(leftByBytes(s3, 5, "GBK"));
System.out.println(leftByBytes(s4, 5, "GBK"));
}}
try {
String[] srcArray = new String[]{"测试数据从左向右取得","0测试数据从左向右取得","0123456789"};
for(int i = 0;i < srcArray.length;i ++){
String result = splitByLength(srcArray[i], 5);
System.out.println("srcArray[ " + i + "] = " + result);
}
//srcArray[ 0] = 测试
//srcArray[ 1] = 0测试
//srcArray[ 2] = 01234
} catch (Exception e) {
e.printStackTrace();
}
} public static String splitByLength(String message, int destLength) throws Exception {
if (message == null || message.length() <= 0 || destLength <= 0) {
return null;
}
message = new String(message.getBytes(), "GB18030");
if (message.length() <= destLength) {
return message;
}
int length = 0;
StringBuffer sb = new StringBuffer();
for (int i = 0; i < message.length(); i++) {
char c = message.charAt(i);
if (isChinese(c)) {
length += 2;
} else {
length += 1;
} if (length > destLength) {
break;
}
sb.append(c);
}
return sb.toString();
} public static boolean isChinese(char message) {
boolean isChinese = false;
if ((message < 0) || (message > 127)) {
isChinese = true;
}
return isChinese;
}
1。首先得理解一般字符编码若是英文字符,首字节首位为0,而非英文和基本字符,首字节的首位为1.
2.从字符串的字节数组开头开始遍历数组,判断每个字节的首位是否为0,若为0循环条件递增1,若为1,循环条件递增2,当递增条件大于等于规定字节长度,跳出循环,并记住最后一次递增是1还是2,然后循环条件跟规定字节长度比较,若长度相等,ok,返回刚才遍历到的字节数组的String,若循环条件跟规定字节长度比较大1,则减去最后一个字符(根据记住最后一次递增是1还是2减去相应字节),转换成String返回。