puzhibing
2023-02-15 2811bab657aab4145b65a45a824fb63e93b58e30
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package com.stylefeng.guns.core.util;
 
import java.util.Random;
 
/***
 * 
 * 得到中文首字母
 * 
 * @author lxm_09
 * 
 */
 
public class PingYinUtil {
 
    public static void main(String[] args) {
        String str = "这是一个测试";
        System.out.println("中文首字母:" + getPYIndexStr(str, true));
    }
 
    /**
     * 返回首字母
     */
    public static String getPYIndexStr(String strChinese, boolean bUpCase) {
        try {
            StringBuffer buffer = new StringBuffer();
            byte b[] = strChinese.getBytes("GBK");// 把中文转化成byte数组
            for (int i = 0; i < b.length; i++) {
                if ((b[i] & 255) > 128) {
                    int char1 = b[i++] & 255;
                    char1 <<= 8;// 左移运算符用“<<”表示,是将运算符左边的对象,向左移动运算符右边指定的位数,并且在低位补零。其实,向左移n位,就相当于乘上2的n次方
                    int chart = char1 + (b[i] & 255);
                    buffer.append(getPYIndexChar((char) chart, bUpCase));
                    continue;
                }
                char c = (char) b[i];
                if (!Character.isJavaIdentifierPart(c))// 确定指定字符是否可以是 Java
                                                        // 标识符中首字符以外的部分。
                    c = 'A';
                buffer.append(c);
            }
            return buffer.toString();
        } catch (Exception e) {
            System.out.println((new StringBuilder()).append("\u53D6\u4E2D\u6587\u62FC\u97F3\u6709\u9519")
                    .append(e.getMessage()).toString());
        }
        return null;
    }
 
    /**
     * 得到首字母
     */
    private static char getPYIndexChar(char strChinese, boolean bUpCase) {
 
        int charGBK = strChinese;
 
        char result;
 
        if (charGBK >= 45217 && charGBK <= 45252)
 
            result = 'A';
 
        else
 
        if (charGBK >= 45253 && charGBK <= 45760)
 
            result = 'B';
 
        else
 
        if (charGBK >= 45761 && charGBK <= 46317)
 
            result = 'C';
 
        else
 
        if (charGBK >= 46318 && charGBK <= 46825)
 
            result = 'D';
 
        else
 
        if (charGBK >= 46826 && charGBK <= 47009)
 
            result = 'E';
 
        else
 
        if (charGBK >= 47010 && charGBK <= 47296)
 
            result = 'F';
 
        else
 
        if (charGBK >= 47297 && charGBK <= 47613)
 
            result = 'G';
 
        else
 
        if (charGBK >= 47614 && charGBK <= 48118)
 
            result = 'H';
 
        else
 
        if (charGBK >= 48119 && charGBK <= 49061)
 
            result = 'J';
 
        else
 
        if (charGBK >= 49062 && charGBK <= 49323)
 
            result = 'K';
 
        else
 
        if (charGBK >= 49324 && charGBK <= 49895)
 
            result = 'L';
 
        else
 
        if (charGBK >= 49896 && charGBK <= 50370)
 
            result = 'M';
 
        else
 
        if (charGBK >= 50371 && charGBK <= 50613)
 
            result = 'N';
 
        else
 
        if (charGBK >= 50614 && charGBK <= 50621)
 
            result = 'O';
 
        else
 
        if (charGBK >= 50622 && charGBK <= 50905)
 
            result = 'P';
 
        else
 
        if (charGBK >= 50906 && charGBK <= 51386)
 
            result = 'Q';
 
        else
 
        if (charGBK >= 51387 && charGBK <= 51445)
 
            result = 'R';
 
        else
 
        if (charGBK >= 51446 && charGBK <= 52217)
 
            result = 'S';
 
        else
 
        if (charGBK >= 52218 && charGBK <= 52697)
 
            result = 'T';
 
        else
 
        if (charGBK >= 52698 && charGBK <= 52979)
 
            result = 'W';
 
        else
 
        if (charGBK >= 52980 && charGBK <= 53688)
 
            result = 'X';
 
        else
 
        if (charGBK >= 53689 && charGBK <= 54480)
 
            result = 'Y';
 
        else
 
        if (charGBK >= 54481 && charGBK <= 55289)
 
            result = 'Z';
 
        else
 
            result = (char) (65 + (new Random()).nextInt(25));
 
        if (!bUpCase)
 
            result = Character.toLowerCase(result);
 
        return result;
 
    }
 
}