?? gfstring.java
字號:
/**
* 在指定字符串插入到源字符串的指定位置
*
* @param src
* 源字符串
* @param insertStr
* 要插入的字符串
* @param index
* 要插入的位置
* @return 插入指定的字符之后的字符串
*/
public static String insert(String src, String insertStr, int index) {
String result = src;
if (src != null && insertStr != null) {
String temp = null;
if (index < 0) {
if (index * -1 > src.length())
result = insertStr + src;
else {
temp = src.substring(src.length() + index + 1);
result = src.substring(0, src.length() + index + 1) + insertStr + temp;
}
} else if (index >= src.length())
result = src + insertStr;
else {
temp = src.substring(index);
result = src.substring(0, index) + insertStr + temp;
}
} else if (src == null && insertStr != null)
result = insertStr;
return result;
}
/**
* 把相臨的兩個字符對換,字符串長度為奇數時最后加“F”
*
* @param src
* @return
*/
public static String interChange(String src) {
String result = null;
if (src != null) {
if (src.length() % 2 != 0)
src += "F";
src += "0";
result = "";
for (int i = 0; i < src.length() - 2; i += 2) {
result += src.substring(i + 1, i + 2);
result += src.substring(i, i + 1);
}
}
return result;
}
/**
* 把數組按指定的編碼方式轉化成字符串
*
* @param b
* 源數組
* @param encoding
* 編碼方式
* @return
*/
public static String bytes2str(byte[] b, String encoding) {
String result = null;
int actualLen = 0;
byte[] ab;
if (b != null && b.length > 0) {
for (int i = 0; i < b.length; i++) {
if (b[i] == 0)
break;
actualLen++;
}
ab = GFCommon.bytesCopy(b, 0, actualLen);
try {
result = new String(ab, encoding);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
return result;
}
/**
* 把一個字符串按指定的長度分割
*
* @param intervalLen
* 間隔長度
* @return
*/
public static String[] split(String src, int intervalLen) {
String[] result = null;
int len = 0;
if (src != null && intervalLen > 0) {
len = src.length() / intervalLen;
if (src.length() % intervalLen != 0)
len++;
result = new String[len];
for (int i = 0, j = 0; i < len - 1; i++, j += intervalLen)
result[i] = src.substring(j, j + intervalLen);
result[len - 1] = src.substring((len - 1) * intervalLen);
}
return result;
}
/**
* 把字節數組轉化成十六進制的字符串
*
* @param b
* @return
*/
public static String bytes2hexstr(byte[] b) {
return bytes2hexstr(b, false);
}
/**
* 把字節數組轉化成十六進制的字符串
* <p>
*
* @param b
* @param highBitFirst
* true:高位優先,即輸出的十六進制字符串是從Byte數組的最大下標開始的
* false:低們優先,即輸出的十六進制字符串是從Byte數組的最小下標0開始的
* @return
*/
public static String bytes2hexstr(byte[] b, boolean highBitFirst) {
String result = null;
if (b != null && b.length > 0) {
if (highBitFirst) {
for (int i = b.length - 1; i >= 0; i--) {
String hex = GFString.byte2hex(b[i]);
if (result == null)
result = hex;
else
result += hex;
}
result = result.toUpperCase();
} else {
for (int i = 0; i < b.length; i++) {
String hex = GFString.byte2hex(b[i]);
if (result == null)
result = hex;
else
result += hex;
}
result = result.toUpperCase();
}
}
return result;
}
/**
* 把字節數組轉化成十六進制的字符串
*
* @param b
* @return
*/
public static String bytes2hexstr(byte[] b, int len) {
String result = null;
if (b != null && b.length > 0 && len <= b.length) {
for (int i = 0; i < len; i++) {
String hex = GFString.byte2hex(b[i]);
if (result == null)
result = hex;
else
result += hex;
}
result = result.toUpperCase();
}
return result;
}
/**
* 把十六進制字符串轉化成字節數組 如果長度不是偶數的話,前面加“0”
*
* @param hexstr
* @return
*/
public static byte[] hexstr2bytes(String hexstr) {
byte[] b = null;
int len = 0;
if (hexstr != null) {
if (hexstr.length() % 2 != 0)
hexstr = "0" + hexstr;
len = hexstr.length() / 2;
b = new byte[len];
String temp = hexstr + "0";
for (int i = 0, j = 0; i < temp.length() - 2; i += 2, j++) {
b[j] = GFString.hex2byte(temp.substring(i, i + 2));
}
}
return b;
}
public static int hexstr2int(String hex) {
if (hex != null && hex.length() <= 8) {
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 0;
}
byte[] b = hexstr2bytes(hex);
return GFCommon.bytes2int(b);
}
return 0;
}
public static String getChineseString(byte[] bArray, String charsetName) {
String ch = null;
if (charsetName != null) {
try {
ch = new String(bArray, charsetName);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
return ch;
}
public static String bytes2str(byte[] b) {
String result = null;
int actualLen = 0;
if (b != null && b.length > 0) {
for (int i = 0; i < b.length; i++) {
if (b[i] == 0)
break;
actualLen++;
}
byte[] b2 = GFCommon.bytesCopy(b, 0, actualLen);
if (b2 != null && b2.length > 0)
result = new String(b2);
}
return result;
}
/**
* 把整數轉換成指定長度的字符串 如果指定長度小于整數的字符串長度,則只取前面LEN個字符。 如果LEN小0,則返回整數的字符串表示型式
*
* @param value
* @param len
* @return
*/
public static String int2str(int value, int len) {
String result = "" + value;
int l = result.length();
if (len >= 0) {
if (l <= len) {
for (int i = 0; i < len - l; i++)
result = "0" + result;
} else {
result = result.substring(0, len);
}
}
return result;
}
/**
* 把十六進制數轉化成字節
*
* @param hex
* @return
*/
public static byte hex2byte(String hex) {
byte b = 0;
int value = 0;
if (hex != null && hex.length() <= 2) {
hex = hex.toUpperCase();
if (hex.length() == 0)
return 0;
else if (hex.length() >= 1) {
value = hex.charAt(0);
if (value < 48 || (value > 57 && value < 65) || value > 70)
return 0;
if (hex.length() == 2) {
value = hex.charAt(1);
if (value < 48 || (value > 57 && value < 65) || value > 70)
return 0;
}
}
try {
b = (byte) Integer.parseInt(hex, 16);
} catch (NumberFormatException e) {
e.printStackTrace();
}
}
return b;
}
/**
* 把字節轉換成十六進制字符串,固定為兩個字符長度
*
* @param b
* @return
*/
public static String byte2hex(byte b) {
String result = null;
result = Integer.toHexString((b >> 4) & 0x0f);
result += Integer.toHexString(b & 0xf);
return result.toUpperCase();
}
/**
* 用UTF-16BE的編碼方式把含有全角編碼的字符串轉成半角編碼的字符串
* <p>
* 比如把“漢##012012YY”轉成“漢##012012YY”
* <p>
* 對全角的空格處理還有問題
*
* @param str
* @return
*/
public static String quan2ban(String str) {
String result = null;
if (str != null) {
try {
byte[] uniBytes = str.getBytes("utf-16be");
byte[] b = new byte[uniBytes.length];
for (int i = 0; i < b.length; i++) {
if (uniBytes[i] == -1) {
b[i] = 0;
if (i + 1 < uniBytes.length)
b[++i] = (byte) (uniBytes[i] + 0x20);
} else
b[i] = uniBytes[i];
}
result = new String(b, "utf-16be");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
return result;
}
/**
* 用UTF-16BE的編碼方式把含有半角的字符串轉成全角字符串
*
* @param str
* @return
*/
public static String ban2quan(String str) {
String result = null;
if (str != null) {
try {
byte[] uniBytes = str.getBytes("utf-16be");
byte[] b = new byte[uniBytes.length];
for (int i = 0; i < b.length; i++) {
if (uniBytes[i] == 0) {
b[i] = -1;
if (i + 1 < uniBytes.length)
b[++i] = (byte) (uniBytes[i] - 0x20);
} else
b[i] = uniBytes[i];
}
result = new String(b, "utf-16be");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
return result;
}
/**
* 用GBK編碼進行全角轉半角
*
* @param str
* @return
*/
public static String quan2banGBK(String str) {
String result = null;
if (str != null) {
try {
int j = 0;
byte[] uniBytes = str.getBytes("GBK");
byte[] b = new byte[uniBytes.length];
for (int i = 0; i < b.length; i++) {
if (uniBytes[i] == (byte) 0xA3) {
if (i + 1 < uniBytes.length)
b[j] = (byte) (uniBytes[++i] - 0x80);
} else {
b[j] = uniBytes[i];
if (uniBytes[i] < 0 && i + 1 < b.length)
b[++j] = uniBytes[++i];
}
j++;
}
result = new String(b, 0, j, "GBK");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
return result;
}
// CDA3 D6B9 20 BABA 23 A3A3 303132 A3B0A3B1A3B259A3D939D3AC39A3D9
/**
* 用GBK編碼進行半角轉全角
* <p>
* 從每個字節判起,如果一個字節的值不大于0X7F,則它是Ascii碼的字符。進入下一個判斷。
* <p>
* 如果一個字節的值大于0X81,且緊跟著它的下一個字節的值在0x40--0xFE之間,則是漢字或全角字符
*
* @param str
* @return
*/
public static String ban2quanGBK(String str) {
String result = null;
if (str != null) {
try {
int j = 0;
byte[] uniBytes = str.getBytes("GBK");
byte[] b = new byte[uniBytes.length * 2];
for (int i = 0; i < uniBytes.length; i++) {
if (uniBytes[i] >= 0) {
b[j] = (byte) 0xA3;
if (j + 1 < b.length)
b[++j] = (byte) (uniBytes[i] + 0x80);
} else {
b[j] = uniBytes[i];
if (i + 1 < uniBytes.length && j + 1 < b.length)
b[++j] = uniBytes[++i];
}
j++;
}
result = new String(b, 0, j, "GBK");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
return result;
}
/**
* 去掉字符串中的空白符
*
* @param s
* @return
*/
public static String removeSpace(String s) {
String rs = null;
String s1 = null;
if (s != null) {
s += " ";
StringBuffer sb = new StringBuffer();
for (int i = 0; i < s.length(); i++) {
s1 = s.substring(i, i + 1);
if (!s1.equals(" "))
sb.append(s1);
}
rs = sb.toString();
}
return rs;
}
/**
* 對字符串中的空格進行格式化,去掉開頭和最后的空格,把字符之間的空格縮減為1個.
* <p>
* 比如:<空格><空格>我是一個人<空格><空格><空格>中國人<空格>大學生<空格><空格>
* <p>
* 結果應該為:我是一個人<空格>中國人<空格>大學生
*
* @param src
* @return
*/
public static String formatSpace(String src) {
String result = null;
if (src != null) {
result = "";
String[] ss = src.split(" ");
for (int i = 0; i < ss.length; i++) {
if (ss[i] != null && ss[i].length() > 0) {
result += ss[i] + " ";
}
}
if (result.length() > 0 && result.substring(result.length() - 1).equals(" "))
result = result.substring(0, result.length() - 1);
}
return result;
}
/**
* 7-BIT編碼 把ASCII碼值最高位為0的字符串進行壓縮轉換成8位二進制表示的字符串
*
* @param src
* @return
*/
public static String encode7bit(String src) {
String result = null;
String hex = null;
byte value;
if (src != null && src.length() == src.getBytes().length) {
result = "";
byte left = 0;
byte[] b = src.getBytes();
for (int i = 0, j = 0; i < b.length; i++) {
j = i & 7;
if (j == 0)
left = b[i];
else {
value = (byte) ((b[i] << (8 - j)) | left);
left = (byte) (b[i] >> j);
hex = GFString.byte2hex((byte) value);
result += hex;
if (i == b.length - 1)
result += GFString.byte2hex(left);
}
}
result = result.toUpperCase();
}
return result;
}
/**
* 對7-BIT編碼進行解碼
*
* @param src
* 十六進制的字符串,且為偶數個
* @return 源字符串
*/
public static String decode7bit(String src) {
String result = null;
int[] b;
String temp = null;
byte srcAscii;
byte left = 0;
if (src != null && src.length() % 2 == 0) {
result = "";
b = new int[src.length() / 2];
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -