java按字節截取帶有漢字的字符串的解法(推舉)。本站提示廣大學習愛好者:(java按字節截取帶有漢字的字符串的解法(推舉))文章只能為提供參考,不一定能成為您想要的結果。以下是java按字節截取帶有漢字的字符串的解法(推舉)正文
因為接口應用的oracle字段長度為固定字節數,然後傳出去的字符串估量比數據庫字段的總字節數要年夜,那末截取小於數據庫字節數的字符串。
本身參考網上的例子,整了個遞歸挪用便可以了,由於截取的字符字節長度必需小與數據庫的字節長度,即假如最初一個字符為漢字,那末只能去失落往前截取。
/**
* 斷定傳出去的字符串,能否
* 年夜於指定的字節,假如年夜於遞歸挪用
* 直到小於指定字節數 ,必定要指定字符編碼,由於各個體系字符編碼都紛歧樣,字節數也紛歧樣
* @param s
* 原始字符串
* @param num
* 傳出去指定字節數
* @return String 截取後的字符串
* @throws UnsupportedEncodingException
*/
public static String idgui(String s,int num)throws Exception{
int changdu = s.getBytes("UTF-8").length;
if(changdu > num){
s = s.substring(0, s.length() - 1);
s = idgui(s,num);
}
return s;
}
java面試題:
編寫一個截取字符串的函數,輸出為一個字符串和字節數,輸入為按字節截取的字符串。然則要包管漢字不被截半個,如"我ABC"4,應當截為"我AB",輸出"我ABC漢DEF",6,應當輸入為"我ABC"而不是"我ABC+漢的半個"。
今朝許多風行的說話,如C#、Java外部采取的都是 Unicode 16(UCS2)編碼,在這類編碼中一切的字符都是兩個字符,是以,假如要截取的字符串是中、英文、數字混雜的,就會發生成績,以下面的字符串:
String s = "a加b等於c,假如a等1、b等於2,那末c等3";
下面的字符串既有漢字,又有英文字符和數字。假如要截取前6個字節的字符,應當是”a加b等",但假如用substring辦法截取前6個字符就成了"a 加b等於c"。發生這個成績的緣由是將substring辦法將雙字節的漢字當做一個字節的字符(UCS2字符)處置了。
英文字母和中文漢字在分歧的編碼格局下,所占用的字節數也是分歧的,我們可以經由過程上面的例子來看看在一些罕見的編碼格局下,一個英文字母和一個中文漢字分離占用若干字節。
import java.io.UnsupportedEncodingException;
public class EncodeTest {
/**
* 打印字符串在指定編碼下的字節數和編碼稱號到掌握台
*
* @param s
* 字符串
* @param encodingName
* 編碼格局
*/
public static void printByteLength(String s, String encodingName) {
System.out.print("字節數:");
try {
System.out.print(s.getBytes(encodingName).length);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
System.out.println(";編碼:" + encodingName);
}
public static void main(String[] args) {
String en = "A";
String ch = "人";
// 盤算一個英文字母在各類編碼下的字節數
System.out.println("英文字母:" + en);
EncodeTest.printByteLength(en, "GB2312");
EncodeTest.printByteLength(en, "GBK");
EncodeTest.printByteLength(en, "GB18030");
EncodeTest.printByteLength(en, "ISO-8859-1");
EncodeTest.printByteLength(en, "UTF-8");
EncodeTest.printByteLength(en, "UTF-16");
EncodeTest.printByteLength(en, "UTF-16BE");
EncodeTest.printByteLength(en, "UTF-16LE");
System.out.println();
// 盤算一個中文漢字在各類編碼下的字節數
System.out.println("中文漢字:" + ch);
EncodeTest.printByteLength(ch, "GB2312");
EncodeTest.printByteLength(ch, "GBK");
EncodeTest.printByteLength(ch, "GB18030");
EncodeTest.printByteLength(ch, "ISO-8859-1");
EncodeTest.printByteLength(ch, "UTF-8");
EncodeTest.printByteLength(ch, "UTF-16");
EncodeTest.printByteLength(ch, "UTF-16BE");
EncodeTest.printByteLength(ch, "UTF-16LE");
}
}
運轉成果以下:
1.英文字母:A
2.字節數:1;編碼:GB2312
3.字節數:1;編碼:GBK
4.字節數:1;編碼:GB18030
5.字節數:1;編碼:ISO-8859-1
6.字節數:1;編碼:UTF-8
7.字節數:4;編碼:UTF-16
8.字節數:2;編碼:UTF-16BE
9.字節數:2;編碼:UTF-16LE
10.中文漢字:人
11.字節數:2;編碼:GB2312
12.字節數:2;編碼:GBK
13.字節數:2;編碼:GB18030
14.字節數:1;編碼:ISO-8859-1
15.字節數:3;編碼:UTF-8
16.字節數:4;編碼:UTF-16
17.字節數:2;編碼:UTF-16BE
18.字節數:2;編碼:UTF-16LE
UTF-16BE和UTF-16LE是UNICODE編碼家族的兩個成員。UNICODE尺度界說了UTF-8、UTF-16、UTF-32三種編碼格局,共有UTF-8、UTF-16、UTF-16BE、UTF-16LE、UTF-32、UTF-32BE、UTF-32LE七種編碼計劃。JAVA所采取的編碼計劃是UTF-16BE。從上例的運轉成果中我們可以看出,GB2312、GBK、GB18030三種編碼格局都可以知足標題的請求。上面我們就以GBK編碼為例來停止解答。
我們不克不及直接應用String類的substring(int beginIndex, int endIndex)辦法,由於它是按字符截取的。'我'和'Z'都被作為一個字符來對待,length都是1。現實上我們只需能辨別開中文漢字和英文字母,這個成績就水到渠成了,而它們的差別就是,中文漢字是兩個字節,英文字母是一個字節。
package com.newyulong.iptv.billing.ftpupload;
import java.io.UnsupportedEncodingException;
public class CutString {
/**
* 斷定能否是一個中文漢字
*
* @param c
* 字符
* @return true表現是中文漢字,false表現是英文字母
* @throws UnsupportedEncodingException
* 應用了JAVA不支撐的編碼格局
*/
public static boolean isChineseChar(char c)
throws UnsupportedEncodingException {
// 假如字節數年夜於1,是漢字
// 以這類方法差別英文字母和中文漢字其實不是非常嚴謹,但在這個標題中,如許斷定曾經足夠了
return String.valueOf(c).getBytes("UTF-8").length > 1;
}
/**
* 按字節截取字符串
*
* @param orignal
* 原始字符串
* @param count
* 截取位數
* @return 截取後的字符串
* @throws UnsupportedEncodingException
* 應用了JAVA不支撐的編碼格局
*/
public static String substring(String orignal, int count)
throws UnsupportedEncodingException {
// 原始字符不為null,也不是空字符串
if (orignal != null && !"".equals(orignal)) {
// 將原始字符串轉換為GBK編碼格局
orignal = new String(orignal.getBytes(), "UTF-8");//
// System.out.println(orignal);
//System.out.println(orignal.getBytes().length);
// 要截取的字節數年夜於0,且小於原始字符串的字節數
if (count > 0 && count < orignal.getBytes("UTF-8").length) {
StringBuffer buff = new StringBuffer();
char c;
for (int i = 0; i < count; i++) {
System.out.println(count);
c = orignal.charAt(i);
buff.append(c);
if (CutString.isChineseChar(c)) {
// 碰到中文漢字,截取字節總數減1
--count;
}
}
// System.out.println(new String(buff.toString().getBytes("GBK"),"UTF-8"));
return new String(buff.toString().getBytes(),"UTF-8");
}
}
return orignal;
}
/**
* 按字節截取字符串
*
* @param orignal
* 原始字符串
* @param count
* 截取位數
* @return 截取後的字符串
* @throws UnsupportedEncodingException
* 應用了JAVA不支撐的編碼格局
*/
public static String gsubstring(String orignal, int count)
throws UnsupportedEncodingException {
// 原始字符不為null,也不是空字符串
if (orignal != null && !"".equals(orignal)) {
// 將原始字符串轉換為GBK編碼格局
orignal = new String(orignal.getBytes(), "GBK");
// 要截取的字節數年夜於0,且小於原始字符串的字節數
if (count > 0 && count < orignal.getBytes("GBK").length) {
StringBuffer buff = new StringBuffer();
char c;
for (int i = 0; i < count; i++) {
c = orignal.charAt(i);
buff.append(c);
if (CutString.isChineseChar(c)) {
// 碰到中文漢字,截取字節總數減1
--count;
}
}
return buff.toString();
}
}
return orignal;
}
/**
* 斷定傳出去的字符串,能否
* 年夜於指定的字節,假如年夜於遞歸挪用
* 直到小於指定字節數
* @param s
* 原始字符串
* @param num
* 傳出去指定字節數
* @return String 截取後的字符串
*/
public static String idgui(String s,int num){
int changdu = s.getBytes().length;
if(changdu > num){
s = s.substring(0, s.length() - 1);
s = idgui(s,num);
}
return s;
}
public static void main(String[] args) throws Exception{
// 原始字符串
String s = "我ZWR愛你們JAVA";
System.out.println("原始字符串:" + s + " : 字節數是: " + s.getBytes().length);
/* System.out.println("截取前1位:" + CutString.substring(s, 1));
System.out.println("截取前2位:" + CutString.substring(s, 2));
System.out.println("截取前4位:" + CutString.substring(s, 4)); */
//System.out.println("截取前12位:" + CutString.substring(s, 12));
System.out.println("截取前12字節:" + CutString.idgui(s, 11));
}
}
以上這篇java按字節截取帶有漢字的字符串的解法(推舉)就是小編分享給年夜家的全體內容了,願望能給年夜家一個參考,也願望年夜家多多支撐。