📄 gfstring.java
字号:
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];
temp = src + "0";
for (int i = 0, j = 0, k = 0; i < temp.length() - 2; i += 2, j++) {
b[j] = Integer.parseInt(temp.substring(i, i + 2), 16);
k = j % 7;
srcAscii = (byte) (((b[j] << k) & 0x7F) | left);
result += (char) srcAscii;
left = (byte) (b[j] >>> (7 - k));
if (k == 6) {
result += (char) left;
left = 0;
}
if (j == src.length() / 2)
result += (char) left;
}
}
return result;
}
/**
* <pre>
* 是否是手机号码
* 1.11位
* 2.是数字
* 3.以"13"开头
* </pre>
*
* @param msg
* @return
*/
public static boolean isMobileNo(String msg) {
// msg = quan2ban(msg);
// msg = removeSpace(msg);
if (msg != null && msg.length() == 11) {
if (isNumeric(msg) && (msg.substring(0, 2).equals("13") || msg.substring(0, 2).equals("15"))) {
return true;
}
}
return false;
}
/**
* <pre>
* 是否是一个电话号码.
* 首先做预处理,把转全角字符转成半角,并把非数字字符去掉,用空格替代
*
* 1.长度要至等于7,但不能超过12
* 2.手机号是一个电话号码
* 3.按空格分隔,长度大于等于3且小于等于12的数字字段至少有一个,且最大不超过2个
* </pre>
*
* @param msg
* @return
*/
public static boolean isTelNo(String msg) {
// msg = quan2banGBK(msg);
// msg = removeSpace(msg);
if (msg != null && msg.length() >= 7) {
String temp = msg + " ";
String t = null;
for (int i = 0; i < temp.length() - 1; i++) {
t = temp.substring(i, i + 1);
if (!isNumeric(t)) {
temp = temp.substring(0, i) + " " + temp.substring(i + 1);
}
}
msg = removeSpace(temp);
if (isNumeric(msg) && msg.length() >= 7 && msg.length() <= 12)
if (msg.substring(0, 1).equals("0")) {
if (msg.length() >= 10)
return true;
} else {
if (isMobileNo(msg))
return true;
else if (msg.length() <= 8)
return true;
}
}
return false;
}
/**
* <pre>
* 得到指定位置前的非空格字符
* 比如:源字符串为:2室一厅,“室”前一个有效字符为2
* 源字符串为:2 室 一厅,“室”前一个有效字符为2
* </pre>
*
* @param msg
* @param index
* @return
*/
public static String getAnteriorNotSpaceChar(String msg, int index) {
String ch = null;
if (msg != null && index > 0) {
for (int i = index - 1; i >= 0; i--) {
String s = msg.substring(i, i + 1);
if (!s.equals(" "))
return s;
}
}
return ch;
}
/**
* 按字符串长度的长短进行排序
* <p>
* 选用快速排序算法
*
* @param list
* @param long2short
* True:从长到短.False:从短到长
* @return
*/
public static ArrayList<String> sortByLen(ArrayList<String> list, boolean long2short) {
ArrayList<String> rs = null;
if (list != null) {
rs = new ArrayList<String>(list.size());
for (String name : list) {
name = GFString.removeSpace(name);
if (name != null && name.length() > 1) {
if (rs.size() > 0) {
for (int i = 0; i < rs.size(); i++) {
if (name.length() >= rs.get(i).length()) {
rs.add(i, name);
break;
} else if (i == rs.size() - 1) {
rs.add(name);
break;
} else
continue;
}
} else
rs.add(name);
}
}
if (!long2short) {
ArrayList<String> rs2 = new ArrayList<String>();
for (String s : rs)
rs2.add(0, s);
rs = rs2;
}
}
return rs;
}
/**
* 把指定位置指定长度的字符用新字符串替换掉
*
* @param 源字符串
* @param index
* 替换字符串的开始下标
* @param len
* 替换的长度
* @param newstr
* 新字符串
* @return
*/
public static String replace(String src, int index, int len, String newstr) {
String result = src;
if (src != null && index >= 0 && index < src.length()) {
if (newstr == null)
newstr = "";
String p1 = src.substring(0, index);
if (index + len >= src.length())
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -