[英]How to trim a String by bytes?
我有一個UTF-8文本,我希望修剪/截斷它的字節,以便我得到一個新的字符串的服裝長度字節。
public static String trimByBytes(String text, int longitudBytes) throws Exception {
byte bytes_text[] = text.getBytes("UTF-8");
int negativeBytes = 0;
byte byte_trimmed[] = new byte[longitudBytes];
if (byte_trimmed.length <= bytes_text.length) {
//copy array manually and count negativeBytes
for (int i = 0; i < byte_trimmed.length; i++) {
byte_trimmed[i] = bytes_text[i];
if (byte_trimmed[i] < 0) {
negativeBytes++;
}
}
//if negativeBytes are odd
if (negativeBytes % 2 != 0 && byte_trimmed[byte_trimmed.length - 1] < 0) {
byte_trimmed[byte_trimmed.length - 1] = 0;//delete last
}
}else{
for (int i = 0; i < bytes_text.length; i++) {
byte_trimmed[i] = bytes_text[i];
}
}
return new String(byte_trimmed);
}
}
例如
創建一個顯式的CharsetDecoder ,並在其上設置CodingErrorAction.IGNORE 。
由於CharsetDecoder與ByteBuffers一起使用,因此應用長度限制就像調用ByteBuffer的limit方法一樣簡單:
String trimByBytes(String str, int lengthOfBytes) {
byte[] bytes = str.getBytes(StandardCharsets.UTF_8);
ByteBuffer buffer = ByteBuffer.wrap(bytes);
if (lengthOfBytes < buffer.limit()) {
buffer.limit(lengthOfBytes);
}
CharsetDecoder decoder = StandardCharsets.UTF_8.newDecoder();
decoder.onMalformedInput(CodingErrorAction.IGNORE);
try {
return decoder.decode(buffer).toString();
} catch (CharacterCodingException e) {
// We will never get here.
throw new RuntimeException(e);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.