獲取所輸入漢字拼音的首字母1

2021-06-13 16:34:13 字數 1119 閱讀 4696

public class stringutil ;  

//存放國標一級漢字不同讀音的起始區位碼對應讀音   

static final char firstletter = ;  

//獲取乙個字串的拼音碼   

public static string getfirstletter(string oristr) ;  

byte unicode = new string(temp).getbytes();  

if (unicode[0] < 128 && unicode[0] > 0) else   

}  

return buffer.tostring();  

}  

/** 獲取乙個漢字的拼音首字母。

* gb碼兩個位元組分別減去160,轉換成10進製碼組合就可以得到區位碼

* 例如漢字「你」的gb碼是0xc4/0xe3,分別減去0xa0(160)就是0x24/0x43

* 0x24轉成10進製就是36,0x43是67,那麼它的區位碼就是3667,在對照表中讀音為『n』

*/  

static char convert(byte bytes)   

secposvalue = bytes[0] * 100 + bytes[1];  

for (i = 0; i < 23; i++)   

}  return result;  

}  public static void main(string args)   

}  

用main函式測試顯示如下:

i love u

sh

i love shdfwf

df

問題:但用android呼叫getfirstletter()方法,虛擬機器上顯示的都為'-'(即無法識別),請問是為什麼?謝謝!

原因:androd預設的字符集是unicode編碼而你的方法處理的是gbk,所以未識別。

解決:將  :byte unicode = new string(temp).getbytes();  

換成:byte unicode = new string(temp).getbytes("gbk");

獲取漢字拼音首字母

有個專案需要用到生成漢字的首字母,但從網上查了一下,對於多音字的處理都不是很好,所以就利用pinyin4j這個工具包自己寫了乙個。用到的jar包是pinyin4j 2.5.0.jar 獲取拼音首字母,多音字用逗號隔開 public static string getfirstspell string...

獲取漢字拼音首字母

unit py inte ce uses sysutils 獲取漢字的拼音首字元,這個函式將用在getpyindexstr 中.function getpyindexchar strchinese string bupcase boolean true char 獲取多個漢字的拼音首字元組成的字串....

獲取漢字拼音首字母

獲取漢字拼音首字母 獲取漢字拼音首字母 很好用.覺的不錯的幫頂 1.下面的不上亂碼是位元組 第一步建立乙個函式 create or replace function f trans pinyin capital p name in varchar2 return varchar2 as v comp...