?? gfstring.java
字號(hào):
/*
* Created on 2004-5-31
*
* To change the template for this generated file go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
package org.ictclas4j.utility;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
/**
* 和字符串相關(guān)的常用操作
*
* @author SINBOY
* @version V1.1
*/
public class GFString {
/**
* 得到一個(gè)十六進(jìn)制字符的二進(jìn)制字符串表示形式
*
* @param hex
* 十六進(jìn)制字符
* @return 二進(jìn)制字符串
*/
public static String hex2bin(String hex) {
if (hex != null) {
HashMap<String, String> map = new HashMap<String, String>(16);
map.put("0", "0000");
map.put("1", "0001");
map.put("2", "0010");
map.put("3", "0011");
map.put("4", "0100");
map.put("5", "0101");
map.put("6", "0110");
map.put("7", "0111");
map.put("8", "1000");
map.put("9", "1001");
map.put("A", "1010");
map.put("B", "1011");
map.put("C", "1100");
map.put("D", "1101");
map.put("E", "1110");
map.put("F", "1111");
return (String) map.get(hex.toUpperCase());
} else
return null;
}
public static String hexstr2bin(String hex) {
String result = null;
if (hex != null) {
if (isHex(hex) == false)
return null;
hex += "0";
result = "";
for (int i = 0; i < hex.length() - 1; i++) {
result += hex2bin(hex.substring(i, i + 1));
}
}
return result;
}
public static boolean isHex(String hex) {
if (hex != null) {
hex = hex.toUpperCase();
for (int i = 0; i < hex.length(); i++) {
int value = hex.charAt(i);
if (value < 48 || (value > 57 && value < 65) || value > 70)
return false;
}
} else
return false;
return true;
}
/**
* 把字符串轉(zhuǎn)化成指定長(zhǎng)度的數(shù)組
*
* @param str
* 要轉(zhuǎn)換的字符串
* @param len
* 指定的轉(zhuǎn)換后的字節(jié)類(lèi)型的數(shù)據(jù)的總長(zhǎng)度
* @param end
* 字節(jié)數(shù)據(jù)的最后一個(gè)字節(jié)所填的數(shù)據(jù)的值
* @return 字節(jié)數(shù)組
*/
public static byte[] getBytes(String str, int start, int len) {
byte[] b = null;
if (str != null) {
byte[] b1 = str.getBytes();
b = GFCommon.bytesCopy(b1, start, len);
}
return b;
}
/**
* 返回按指定編碼方式編碼的字符串
*
* @param bArray
* 字節(jié)數(shù)組
* @param charsetName
* 字符集
* @return
*/
public static String getEncodedString(byte[] bArray, String charsetName) {
String ch = null;
if (charsetName != null) {
try {
ch = new String(bArray, charsetName);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
return ch;
}
/**
* 把表示一個(gè)數(shù)的十六進(jìn)制的字符串轉(zhuǎn)化成十進(jìn)制的數(shù)
*
* @param hex
* 十六進(jìn)制字符串
* @return 十進(jìn)制的整數(shù)
*/
public static long hexstr2long(String hex) {
long value = 0;
if (hex != null) {
hex = hex.toUpperCase();
if (hex.length() > 16)
hex = hex.substring(0, 16);
if (isHex(hex)) {
byte[] b = hexstr2bytes(hex);
value = GFCommon.bytes2long(b);
}
}
return value;
}
/**
* 把字符串轉(zhuǎn)化成固定長(zhǎng)的字符串。如果不夠指定的長(zhǎng)度,在前面添加指定的字符; 如果大于指定的長(zhǎng)度,把后面多出的去掉。
*
* @param str
* 要轉(zhuǎn)換的字符串
* @param len
* 轉(zhuǎn)換后的長(zhǎng)度
* @param appendChar
* 添加的字符
* @return 轉(zhuǎn)換后的字符串
*/
public static String getFixedLenStr(String str, int len, char appendChar) {
if (str == null || len < 0)
return null;
else {
int strLen = 0;
strLen = str.length();
if (len <= strLen) {
str = str + appendChar;
return str.substring(0, len);
} else {
for (int i = 0; i < len - strLen; i++)
str = appendChar + str;
return str;
}
}
}
/**
* 把一個(gè)二進(jìn)制字符串的轉(zhuǎn)化成一個(gè)整數(shù)
*
* @param bs
* 二進(jìn)制字符串
* @return 二進(jìn)制字符串表示的值
*/
public static long bin2long(String bs) {
long value = 0;
if (bs != null && bs.length() <= 64) {
byte[] b = bin2bytes(bs);
value = GFCommon.bytes2long(b);
}
return value;
}
public static String bin2hex(String bin) {
String hex = null;
HashMap<String, String> map = new HashMap<String, String>(16);
map.put("0000", "0");
map.put("0001", "1");
map.put("0010", "2");
map.put("0011", "3");
map.put("0100", "4");
map.put("0101", "5");
map.put("0110", "6");
map.put("0111", "7");
map.put("1000", "8");
map.put("1001", "9");
map.put("1010", "A");
map.put("1011", "B");
map.put("1100", "C");
map.put("1101", "D");
map.put("1110", "E");
map.put("1111", "F");
if (bin != null && bin.length() <= 4) {
if (isBinstr(bin)) {
for (int i = 0; i < 4 - bin.length(); i++)
bin = "0" + bin;
hex = (String) map.get(bin);
}
}
return hex;
}
public static String bin2hexstr(String bin) {
String hex = null;
if (bin != null) {
if (isBinstr(bin)) {
int ys = bin.length() % 4;
for (int i = 0; ys != 0 && i < 4 - ys; i++)
bin = "0" + bin;
bin += "0";
hex = "";
for (int i = 0; i < bin.length() - 4; i += 4) {
String h = bin2hex(bin.substring(i, i + 4));
if (h != null) {
if (h.equals("0")) {
if (!hex.equals(""))
hex += h;
} else
hex += h;
}
}
if (hex.equals(""))
hex = "0";
}
}
return hex;
}
public static byte bin2byte(String bin) {
byte b = 0;
if (bin != null && bin.length() <= 8) {
if (isBinstr(bin)) {
String hex = bin2hexstr(bin);
b = hex2byte(hex);
}
}
return b;
}
public static byte[] bin2bytes(String bin) {
byte[] bs = null;
if (bin != null) {
String hex = bin2hexstr(bin);
bs = hexstr2bytes(hex);
}
return bs;
}
public static int bin2int(String bin) {
int value = 0;
if (bin != null && bin.length() <= 32) {
if (isBinstr(bin)) {
String hex = bin2hexstr(bin);
value = hexstr2int(hex);
}
}
return value;
}
public static boolean isBinstr(String bin) {
boolean result = false;
if (bin != null) {
byte[] b = bin.getBytes();
for (int i = 0; i < b.length; i++) {
if (b[i] != 48 && b[i] != 49)
return false;
}
return true;
}
return result;
}
/**
* 判斷一個(gè)字符串是否是數(shù)字
*
* @param str
* @return
*/
public static boolean isNumeric(String str) {
if (str != null) {
try {
str = str.trim();
double d = Double.parseDouble(str);
d = d + 1;
return true;
} catch (NumberFormatException e) {
}
}
return false;
}
/**
* 判斷字符串是否全是漢字
*
* @param str
* @return
*/
public static boolean isAllChinese(String str) {
if (str != null) {
str = quan2ban(str);
if (str != null) {
if (str.length() * 2 == str.getBytes().length)
return true;
}
}
return false;
}
/**
* 判斷字符串是否全不是漢字
*
* @param str
* @return
*/
public static boolean isNoChinese(String str) {
if (str != null) {
str = quan2ban(str);
if (str != null) {
if (str.length() == str.getBytes().length)
return true;
}
}
return false;
}
/**
* 是否是字母
*
* @param str
* @return
*/
public static boolean isLetter(String str) {
if (str != null) {
byte b[];
str = str.trim();
b = str.toUpperCase().getBytes();
for (int i = 0; i < b.length; i++) {
if (b[i] < 65 || b[i] > 90)
return false;
}
return true;
}
return false;
}
/**
* 把一個(gè)整數(shù)轉(zhuǎn)化成8位二進(jìn)制字符串的表示形式
*
* @param value
* 0--256之間的整數(shù)
* @return 長(zhǎng)度為8的二進(jìn)制字符串
*/
public static String int2bin(int value) {
if (value >= 0 && value < 256) {
String bin = Integer.toBinaryString(value);
int len = bin.length();
for (int i = 0; i < 8 - len; i++)
bin = "0" + bin;
return bin;
}
return null;
}
/**
* 把表示數(shù)字含義的字符串轉(zhuǎn)你成整形
*
* @param str
* 要轉(zhuǎn)換的字符串
* @return 如果是有意義的整數(shù),則返回此整數(shù)值。否則,返回-1。
*/
public static int cint(String str) {
if (str != null)
try {
int i = new Integer(str).intValue();
return i;
} catch (NumberFormatException e) {
}
return -1;
}
public static long clong(String str) {
if (str != null)
try {
return new Long(str).longValue();
} catch (NumberFormatException e) {
}
return -1;
}
/**
* 在一個(gè)字符串中取出指定的子字符串/
*
* @param str
* 字符串
* @param begin
* 開(kāi)始位置,從0數(shù)起
* @param len
* 子字符串的長(zhǎng)度
* @return 子字符串
*/
public static String substr(String str, int begin, int len) {
if (str == null)
return null;
else {
int strLen = 0;
strLen = str.length();
if (begin >= strLen)
return null;
else {
if (len > strLen)
return null;
else {
str += "0";
try {
return str.substring(begin, len);
} catch (IndexOutOfBoundsException e) {
return null;
}
}
}
}
}
/**
* 把字節(jié)數(shù)組轉(zhuǎn)化成十六進(jìn)制的字符串
*
* @param bs
*/
public static String bytes2hex(byte[] b) {
String result = "";
int value;
if (b != null && b.length > 0)
for (int i = 0; i < b.length; i++) {
value = (b[i] >>> 4) & 0x0F;
result += Integer.toHexString(value);
value = b[i] & 0x0F;
result += Integer.toHexString(value);
}
return result.toUpperCase();
}
/**
* 把UNICODE編碼的字符串轉(zhuǎn)化成漢字編碼的字符串
*
* @param hexString
* @return
*/
public static String unicode2gb(String hexString) {
StringBuffer sb = new StringBuffer();
if (hexString == null)
return null;
for (int i = 0; i + 4 <= hexString.length(); i = i + 4) {
try {
int j = Integer.parseInt(hexString.substring(i, i + 4), 16);
sb.append((char) j);
} catch (NumberFormatException e) {
return hexString;
}
}
return sb.toString();
}
/**
* 把漢字轉(zhuǎn)化成UNICODE編碼的字符串
*
* @param gbString
* @return
*/
public static String gb2unicode(String gbString) {
String result = "";
char[] c;
int value;
if (gbString == null)
return null;
// if (gbString.getBytes().length == gbString.length())
// return gbString;
String temp = null;
c = new char[gbString.length()];
StringBuffer sb = new StringBuffer(gbString);
sb.getChars(0, sb.length(), c, 0);
for (int i = 0; i < c.length; i++) {
value = (int) c[i];
// System.out.println("[" + i + "]:" +value );
// System.out.println("hex:"+Integer.toHexString(value));
temp = Integer.toHexString(value);
result += fill(temp, 4);
}
return result.toUpperCase();
}
/**
* 如果字符串的長(zhǎng)度沒(méi)有達(dá)到指定的長(zhǎng)度,則在字符串前加“0”補(bǔ)夠指定的長(zhǎng)度
*
* @param src
* 原先的字符串
* @param len
* 指定的長(zhǎng)度
* @return 指定長(zhǎng)度的字符串
*/
public static String fill(String src, int len) {
String result = null;
if (src != null && src.length() <= len) {
result = src;
for (int i = 0; i < len - src.length(); i++) {
result = "0" + result;
}
}
return result;
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -