?? stringutils.java
字號:
}
/**
* 大小寫無關的字符串替換策略
* @param str
* @param src
* @param obj
* @return
*/
public static String replaceIgnoreCase(String str, String src, String obj){
String l_str = str.toLowerCase();
String l_src = src.toLowerCase();
int fromIdx = 0;
StringBuffer result = new StringBuffer();
do{
int idx = l_str.indexOf(l_src, fromIdx);
if(idx==-1)
break;
result.append(str.substring(fromIdx, idx));
result.append(obj);
fromIdx = idx + src.length();
}while(true);
result.append(str.substring(fromIdx));
return result.toString();
}
/**
* 根據漢字字符獲得筆畫數,拼音和非法字符默認為0
* @param charcator
* @return int
*/
public static int getStrokeCount(char charcator) {
byte[] bytes = (String.valueOf(charcator)).getBytes();
if (bytes == null || bytes.length > 2 || bytes.length <= 0) {
// 錯誤引用,非合法字符
return 0;
}
if (bytes.length == 1) {
// 英文字符
return 0;
}
if (bytes.length == 2) {
// 中文字符
int highByte = 256 + bytes[0];
int lowByte = 256 + bytes[1];
return getStrokeCount(highByte, lowByte);
}
// 未知錯誤
return 0;
}
/**
* @param highByte 高位字節
* @param lowByte 低位字節
* @return int
*/
private static int getStrokeCount(int highByte, int lowByte) {
if (highByte < 0xB0 || highByte > 0xF7 || lowByte < 0xA1
|| lowByte > 0xFE) {
// 非GB2312合法字符
return -1;
}
int offset = (highByte - 0xB0) * (0xFE - 0xA0) + (lowByte - 0xA1);
return Constants.gb2312StrokeCount[offset];
}
/**
* 該方法返回一個字符串的拼音,對于要做敏感字
* 檢查時應該一個字一個字來獲取其拼音以免無法
* 得知每個字對應的拼音。
* @param word
* @return String
*/
public static String getPinyin(String word) {
String pinyin = "";
for (int i = 0; i < word.length(); i++)
pinyin += getPinyin2(getCode(word.charAt(i)));
return pinyin;
}
/**
* 該方法返回一個字符的DBCS編碼值
* @param cc
* @return int
*/
protected static int getCode(char cc) {
byte[] bs = String.valueOf(cc).getBytes();
int code = (bs[0] << 8) | (bs[1] & 0x00FF);
if(bs.length < 2)
code = (int)cc;
bs = null;
return code;
}
/**
* 該方法通過DBCS的編碼值到哈希表中查詢得到對應的拼音串
* @param hz
* @return String
*/
protected static String getPinyin2(int hz) {
String py = "";
if (hz > 0 && hz < 160)
py += hz;
//else if (hz < -20319 || hz > -10247);
else if (hz <= -10247 && hz >= -20319){
PinyinCode pc = null;
int i = Constants.pinyin.size() - 1;
for (; i >= 0; i--) {
pc = (PinyinCode) Constants.pinyin.get(i);
if (pc.code <= hz)
break;
}
if (i >= 0)
py = pc.pinyin;
}
return py;
}
/**
* 用戶名必須是數字或者字母的結合
* @param username
* @return
*/
public static boolean isLegalUsername(String username) {
for(int i=0;i<username.length();i++){
char ch = username.charAt(i);
if(!isAscii(ch)&&
ch != '.' &&
ch != '_' &&
ch != '-' &&
ch != '+' &&
ch != '(' &&
ch != ')' &&
ch != '*' &&
ch != '^' &&
ch != '@' &&
ch != '%' &&
ch != '$' &&
ch != '#' &&
ch != '~' &&
ch != '-')
return false;
}
return true;
}
/**
* 判斷是否是字母和數字的結合
* @param name
* @return
*/
public static boolean isAsciiOrDigit(String name){
for(int i=0;i<name.length();i++){
char ch = name.charAt(i);
if(!isAscii(ch))
return false;
}
return true;
}
public static boolean isAscii(char ch){
return (ch >='a' && ch <='z') || (ch >='A' && ch <='Z') || (ch >='0' && ch <='9');
}
/**
* 返回姓名的拼音首字母
* @param username
* @return
*/
public static String getTxlUserPinyin(String username) {
if(username.getBytes().length == (2 * username.length())){
//純中文
StringBuffer pinyin = new StringBuffer();
for(int i=0;i<username.length();i++){
String py = StringUtils.getPinyin(String.valueOf(username.charAt(i)));
if(py!=null && py.length()>0)
pinyin.append(py.charAt(0));
else
pinyin.append('V');
}
return pinyin.toString().toUpperCase();
}
else
if(username.getBytes().length == username.length()){
int len = (username.length()>3)?3:username.length();
return username.substring(0,len).toUpperCase();
}
else{
StringBuffer pinyin = new StringBuffer();
for(int i=0;i<username.length();i++){
char ch = username.charAt(i);
try{
String py = StringUtils.getPinyin(String.valueOf(ch));
if(py!=null && py.length()>0)
pinyin.append(py.charAt(0));
else
pinyin.append(ch);
}catch(ArrayIndexOutOfBoundsException e){}
if(pinyin.length()>=3)
break;
}
return pinyin.toString().toUpperCase();
}
}
}
/**
* BASE64編碼解碼實現類
* @author liusoft
* created on 2002-12-19
*/
class Base64Code {
protected static byte[] _encode_map =
{
(byte)'A', (byte)'B', (byte)'C', (byte)'D', (byte)'E', (byte)'F', (byte)'G',
(byte)'H', (byte)'I', (byte)'J', (byte)'K', (byte)'L', (byte)'M', (byte)'N',
(byte)'O', (byte)'P', (byte)'Q', (byte)'R', (byte)'S', (byte)'T', (byte)'U',
(byte)'V', (byte)'W', (byte)'X', (byte)'Y', (byte)'Z',
(byte)'a', (byte)'b', (byte)'c', (byte)'d', (byte)'e', (byte)'f', (byte)'g',
(byte)'h', (byte)'i', (byte)'j', (byte)'k', (byte)'l', (byte)'m', (byte)'n',
(byte)'o', (byte)'p', (byte)'q', (byte)'r', (byte)'s', (byte)'t', (byte)'u',
(byte)'v', (byte)'w', (byte)'x', (byte)'y', (byte)'z',
(byte)'0', (byte)'1', (byte)'2', (byte)'3', (byte)'4',
(byte)'5', (byte)'6', (byte)'7', (byte)'8', (byte)'9',
(byte)'+', (byte)'/' };
protected static byte _decode_map[] = new byte[128];
static {
/*
* Fill in the decode map
*/
for (int i = 0; i < _encode_map.length; i++) {
_decode_map[_encode_map[i]] = (byte)i;
}
}
/**
* This class isn't meant to be instantiated.
*/
private Base64Code ()
{
}
/**
* This method encodes the given byte[] using the Base64 encoding
*
*
* @param data the data to encode.
* @return the Base64 encoded <var>data</var>
*/
public final static byte[] encode (byte[] data) {
if (data == null) {
return (null);
}
/*
* Craete a buffer to hold the results
*/
byte dest[] = new byte[((data.length + 2) / 3) * 4];
/*
* 3-byte to 4-byte conversion and
* 0-63 to ascii printable conversion
*/
int i, j;
int data_len = data.length - 2;
for (i = 0, j = 0; i < data_len; i += 3) {
dest[j++] = _encode_map[(data[i] >>> 2) & 077];
dest[j++] = _encode_map[(data[i + 1] >>> 4) & 017 |
(data[i] << 4) & 077];
dest[j++] = _encode_map[(data[i + 2] >>> 6) & 003 |
(data[i + 1] << 2) & 077];
dest[j++] = _encode_map[data[i + 2] & 077];
}
if (i < data.length) {
dest[j++] = _encode_map[(data[i] >>> 2) & 077];
if (i < data.length-1) {
dest[j++] = _encode_map[(data[i + 1] >>> 4) & 017 |
(data[i] << 4) & 077];
dest[j++] = _encode_map[(data[i + 1] << 2) & 077];
} else {
dest[j++] = _encode_map[(data[i] << 4) & 077];
}
}
/*
* Pad with "=" characters
*/
for ( ; j < dest.length; j++) {
dest[j] = (byte)'=';
}
return (dest);
}
/**
* This method decodes the given byte[] using the Base64 encoding
*
*
* @param data the Base64 encoded data to decode.
* @return the decoded <var>data</var>.
*/
public final static byte[] decode (byte[] data) {
if (data == null)
return (null);
/*
* Remove the padding on the end
*/
int ending = data.length;
if (ending < 1) {
return (null);
}
while (data[ending - 1] == '=')
ending--;
/*
* Create a buffer to hold the results
*/
byte dest[] = new byte[ending - data.length / 4];
/*
* ASCII printable to 0-63 conversion
*/
for (int i = 0; i < data.length; i++) {
data[i] = _decode_map[data[i]];
}
/*
* 4-byte to 3-byte conversion
*/
int i, j;
int dest_len = dest.length - 2;
for (i = 0, j = 0; j < dest_len; i += 4, j += 3) {
dest[j] = (byte) (((data[i] << 2) & 255) |
((data[i + 1] >>> 4) & 003));
dest[j + 1] = (byte) (((data[i + 1] << 4) & 255) |
((data[i + 2] >>> 2) & 017));
dest[j + 2] = (byte) (((data[i + 2] << 6) & 255) |
(data[i + 3] & 077));
}
if (j < dest.length) {
dest[j] = (byte) (((data[i] << 2) & 255) |
((data[i + 1] >>> 4) & 003));
}
j++;
if (j < dest.length) {
dest[j] = (byte) (((data[i + 1] << 4) & 255) |
((data[i + 2] >>> 2) & 017));
}
return (dest);
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -