---
title: "一个有趣的汉字转成拼音声母的程序[转]"
author: "lin5061"
date: "2007-05-28T08:48:40Z"
tags: []
slug: "untitled"
draft: false
lastmod: "2009-03-05T10:55:00Z"
id: 88286
url: "https://lin5061.iteye.com/blog/88286"
---

import java.lang.\*;

public class GB2Alpha {

    //字母Z使用了两个标签，这里有２７个值  
    //i, u, v都不做声母, 跟随前面的字母  
    private char\[] chartable =  
            {  
                '啊', '芭', '擦', '搭', '蛾', '发', '噶', '哈', '哈',  
                '击', '喀', '垃', '妈', '拿', '哦', '啪', '期', '然',  
                '撒', '塌', '塌', '塌', '挖', '昔', '压', '匝', '座'  
            };

    private char\[] alphatable =  
            {  
                'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',

                'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'  
            };

    private int\[] table = new int\[27];

    //初始化  
    {  
        for (int i = 0; i &lt; 27; ++i) {  
            table\[i] = gbValue(chartable\[i]);  
        }  
    }

    public GB2Alpha() {

    }

    //主函数,输入字符,得到他的声母,  
    //英文字母返回对应的大写字母  
    //其他非简体汉字返回 '0'

    public char Char2Alpha(char ch) {

        if (ch &gt;= 'a' && ch &lt;= 'z')  
            return (char) (ch - 'a' + 'A');  
        if (ch &gt;= 'A' && ch &lt;= 'Z')  
            return ch;

        int gb = gbValue(ch);  
        if (gb &lt; table\[0])  
            return '0';

        int i;  
        for (i = 0; i &lt; 26; ++i) {  
            if (match(i, gb))  
                break;  
        }

        if (i &gt;= 26)  
            return '0';  
        else  
            return alphatable\[i];  
    }

    //根据一个包含汉字的字符串返回一个汉字拼音首字母的字符串  
    public String String2Alpha(String SourceStr) {  
        String Result = "";  
        int StrLength = SourceStr.length();  
        int i;  
        try {  
            for (i = 0; i &lt; StrLength; i++) {  
                Result += Char2Alpha(SourceStr.charAt(i));  
            }  
        } catch (Exception e) {  
            Result = "";  
        }  
        return Result;  
    }

    private boolean match(int i, int gb) {  
        if (gb &lt; table\[i])  
            return false;

        int j = i + 1;

        //字母Z使用了两个标签  
        while (j &lt; 26 && (table\[j] == table\[i]))  
            ++j;

        if (j == 26)  
            return gb &lt;= table\[j];  
        else  
            return gb &lt; table\[j];

    }

    //取出汉字的编码  
    private int gbValue(char ch) {  
        //System.out.println("ch--&gt;"+ch);  
        String str = new String();  
        str += ch;  
        try {  
            byte\[] bytes = str.getBytes("GB2312");  
            if (bytes.length &lt; 2)  
                return 0;  
            return (bytes\[0] &lt;&lt; 8 & 0xff00) + (bytes\[1] &  
                    0xff);  
        } catch (Exception e) {  
            return 0;  
        }

    }  
    public static void main(String\[] args){  
      GB2Alpha g = new GB2Alpha();  
      System.out.println("高手--&gt;"+g.String2Alpha("高手"));  
       
    }  
}

 

出处:http://yangjun1120.spaces.live.com/blog/cns!F9D66F146BA5A776!184.entry#post