C++:截取字串中的漢字

來源:互聯網
上載者:User
1、 const char *str = "test測試test" ; while (*str) { //這裡只需要判斷第一個位元組大於0x80就行了,前提是輸入的是合法的GBK字串 //原因在於,如果第一個位元組大於0x80,那麼它必然和後面一個位元組一起組成一個漢字 //所以就沒有必要再去判斷後面一個位元組了 //再強調一下,前提條件是輸入合法的GBK字串 if (*str > 0x80) { // 漢字,計數器++ str += 2; //是漢字自然就該直接+2了 } else { str++; } }  2、

參看下面的字串轉換函式。

/**
* 用getBytes(encoding):返回字串的一個byte數組

* 當b[0]為 63時,應該是轉碼錯誤
* A、不亂碼的漢字字串:

* 1、encoding用GB2312時,每byte是負數;
*
2、encoding用ISO8859_1時,b[i]全是63。 

* B、亂碼的漢字字串:
*
1、encoding用ISO8859_1時,每byte也是負數;
*
2、encoding用GB2312時,b[i]大部分是63。
* C、英文字串
*
1、encoding用ISO8859_1和GB2312時,每byte都大於0;
*
總結:給定一個字串,用getBytes("iso8859_1")
* 1、如果b[i]有63,不用轉碼; A-2

* 2、如果b[i]全大於0,那麼為英文字串,不用轉碼; B-1
*
3、如果b[i]有小於0的,那麼已經亂碼,要轉碼。 C-1
*/
private static
String toGb2312(String str) {
if (str == null)
return null;
String retStr = str;

byte b[];
try {
b =
str.getBytes("ISO8859_1");

for (int i = 0; i < b.length; i++) {

byte b1 = b[i];

if (b1 == 63)

break; //1

else if (b1 > 0)

continue;//2

else if (b1 < 0) {
//不可能為0,0為字串結束符
retStr = new
String(b, "GB2312");
break;

}
}

} catch (UnsupportedEncodingException e) {

// e.printStackTrace();
}

return retStr;

 

 

3、

unsigned char *str = "test測試test" ; int length; int i;  length = strlen (str); for (i = 0; i < length - 1; i++) { if ( *str >= 0x81 && *str <= 0xFE && *(str + 1) >= 0x40 && *(str + 1) <= 0xFE) { // 漢字 } } unsignedchar*str="test測試test";//把字串換成“漢A”試試,結果為2

有人說:“一個GBK漢字要佔兩個char空間(二位元組),而且第一個位元組裡的值是小於0的。可以據此判斷是否為漢字。”
1、為什麼第一個位元組的值小於0呢?
2、如果僅通過判斷第一個位元組如果小於0,則該位元組和下一個位元組就組成一個漢字,這種邏輯是否保險?
3、因為還看到有人說,GBK編碼的漢字有高位和低位兩位,第一個是低位吧?需要第一個位元組在160-254之間,第二個位元組在64-254之間,這樣是不是比2中提到的方法要保險?
4、如果DB中的字元集是SIMPLIFIED CHINESE_CHINA.ZHS16GBK,這個是GBK字元集?GBK相容GB2312 似乎有些字元集中有些漢字佔三個位元組 “通過判斷第一個位元組如果小於0,則該位元組和下一個位元組就組成一個漢字” //GBK漢字內碼範圍
//81-A0 ,40-7E 80-FE
//AA-AF ,40-7E 80-A0
//B0-D6 ,40-7E 80-FE
//D7 ,40-7E 80-F9
//D8-F7 ,40-7E 80-FE
//F8-FE ,40-7E 80-A0例如://81-A0 ,40-7E 80-FE
表示字元的ascii碼要在129-160,64-126,128-254這三個區間段內  4、

在工作中,遇到要截取字串在螢幕上顯示出來,因為字串帶有漢字,如果截取不好,會引起亂碼,寫了下面的函數

在uclinux下與VC6.0中測試可以通過。

view plaincopy to clipboardprint?

   1. /*截取字串

   2. name :要截取的字串

   3. store:要儲存的字串

   4. len:要截取的長度

   5. */ 

   6. void split_name( char * name , char * store , int len ) 

   7. { 

   8.     int i= 0 ; 

   9.     char strTemp[L(NAMEL)]={0}; 

  10.      

  11.     if ( strlen(name)
  12.     { 

  13.         strcpy( store, name );  *name=0; 

  14.         return ; 

  15.     } 

  16.  

  17.     //從第1個位元組開始判斷 

  18.     while( i < len ) 

  19.     { 

  20.         if ( name[i]>>7&1 && name[i+1]>>7&1 )       //if ( name[i] < 0 && name[i+1] < 0 ) 

  21.             i = i + 2 ; 

  22.         else 

  23.             i = i + 1 ; 

  24.     } 

  25.     i = i > len ? i-3 :i-1; 

  26.     strncpy( store , name , i+1 ); //截取前i+1位 

  27.     *(store+i+1)=0; 

  28.     strcpy( strTemp , name + i + 1 ); 

  29.     strcpy( name , strTemp ); 

  30. } 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.