Java工具类库存
字符串操作
-1.判断字符串是否为全数字
public static boolean isNum(String str){
return str.matches("^[-+]?(([0-9]+)([.]([0-9]+))?|([.]([0-9]+))?)$");
}
-2.判断字符串是否以字母开头
public static boolean isBeginWithCharacter(String str){
return str.matches("^[a-z,A-Z].*$");
}
-3.判断字符串是否包含汉字
public static boolean isContainChinese(String str) {
Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
Matcher m = p.matcher(str);
if (m.find()) {
return true;
}
return false;
}
-4.随机生成单个简体汉字字符
private static char getChineseChar() {
String str = "";
int hightPos;
int lowPos;
Random random = new Random();
hightPos = (176 + Math.abs(random.nextInt(39)));
lowPos = (161 + Math.abs(random.nextInt(93)));
byte[] b = new byte[2];
b[0] = (Integer.valueOf(hightPos)).byteValue();
b[1] = (Integer.valueOf(lowPos)).byteValue();
try {
str = new String(b, "GBK");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return str.charAt(0);
}
-5.Base64算法对字符串进行加密解密
/**
* encodeBase64("HelloWorld") return SGVsbG9Xb3JsZA==
* decodeBase64(encodeBase64("HelloWorld")) return HelloWorld
*/
/** Base64 加密用 */
private static final int fillchar = '=';
private static final String cvt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz" + "0123456789+/";
/**
* @Description Base64 解密
* @param str:要解密的Base64字符串
* @return:解密后的字符串
*/
public static String decodeBase64(String str) {
byte[] data = str.getBytes();
int c, c1;
int len = data.length;
StringBuffer ret = new StringBuffer((len * 3) / 4);
for (int i = 0; i < len; ++i) {
c = cvt.indexOf(data[i]);
++i;
c1 = cvt.indexOf(data[i]);
c = ((c << 2) | ((c1 >> 4) & 0x3));
ret.append((char) c);
if (++i < len) {
c = data[i];
if (fillchar == c)
break;
c = cvt.indexOf((char) c);
c1 = ((c1 << 4) & 0xf0) | ((c >> 2) & 0xf);
ret.append((char) c1);
}
if (++i < len) {
c1 = data[i];
if (fillchar == c1)
break;
c1 = cvt.indexOf((char) c1);
c = ((c << 6) & 0xc0) | c1;
ret.append((char) c);
}
}
return ret.toString();
}
/**
* @Description Base64加密字符串
* @param str:要加密的字符串
* @return:加密后的字符串
*/
public static String encodeBase64(String str) {
byte[] data = str.getBytes();
int c;
int len = data.length;
StringBuffer ret = new StringBuffer(((len / 3) + 1) * 4);
for (int i = 0; i < len; ++i) {
c = (data[i] >> 2) & 0x3f;
ret.append(cvt.charAt(c));
c = (data[i] << 4) & 0x3f;
if (++i < len)
c |= (data[i] >> 4) & 0x0f;
ret.append(cvt.charAt(c));
if (i < len) {
c = (data[i] << 2) & 0x3f;
if (++i < len)
c |= (data[i] >> 6) & 0x03;
ret.append(cvt.charAt(c));
} else {
++i;
ret.append((char) fillchar);
}
if (i < len) {
c = data[i] & 0x3f;
ret.append(cvt.charAt(c));
} else {
ret.append((char) fillchar);
}
}
return ret.toString();
}
-6.判断输入char类型变量的字符类型 汉字 英文 数字型等
public static enum CharType {
DELIMITER,
NUM,
LETTER,
OTHER,
CHINESE;
}
/**
* 判断输入char类型变量的字符类型 汉字 英文 数字型等
*checkCharType('a').name() return LETTER
*checkCharType('A').name() return LETTER
*checkCharType('1').name() return NUM
*checkCharType('1').name() return NUM
*checkCharType('*').name() return DELIMITER
*checkCharType('好').name() return CHINESE
*/
public static CharType checkCharType(char c) {
CharType ct = null;
if ((c >= 0x4e00) && (c <= 0x9fbb)) {
ct = CharType.CHINESE;
}
else if ((c >= 0xff00) && (c <= 0xffef)) {
if (((c >= 0xff21) && (c <= 0xff3a))
|| ((c >= 0xff41) && (c <= 0xff5a))) {
ct = CharType.LETTER;
}
else if ((c >= 0xff10) && (c <= 0xff19)) {
ct = CharType.NUM;
}
else
ct = CharType.DELIMITER;
}
else if ((c >= 0x0021) && (c <= 0x007e)) {
if ((c >= 0x0030) && (c <= 0x0039)) {
ct = CharType.NUM;
}
else if (((c >= 0x0041) && (c <= 0x005a))
|| ((c >= 0x0061) && (c <= 0x007a))) {
ct = CharType.LETTER;
}
else
ct = CharType.DELIMITER;
}
else if ((c >= 0x00a1) && (c <= 0x00ff)) {
if ((c >= 0x00c0) && (c <= 0x00ff)) {
ct = CharType.LETTER;
} else
ct = CharType.DELIMITER;
} else
ct = CharType.OTHER;
return ct;
}
-7.判断字符串是否都为字母
public static boolean isAlpha(String str) {
if (str == null) {
return false;
}
int length = str.length();
for (int i = 0; i < length; i++) {
if (!Character.isLetter(str.charAt(i))) {
return false;
}
}
return true;
}
-8.判断参数字符串是否符合邮箱字符串格式
public static boolean isValidEmail(String paramString) {
String regex = "[a-zA-Z0-9_\\.]{1,}@(([a-zA-z0-9]-*){1,}\\.){1,3}[a-zA-z\\-]{1,}";
if (paramString.matches(regex)) {
return true;
} else {
return false;
}
}
-9.判断参数字符串是否符合手机号码字符串格式
public static boolean isValidMobiNumber(String paramString) {
String regex = "^1\\d{10}$";
if (paramString.matches(regex)) {
return true;
}
return false;
}
-10.随机取得指定[min,max)的整数
public static int getRandom(int min, int max) {
if (min > max) {
return 0;
}
if (min == max) {
return min;
}
return min + new Random().nextInt(max - min);
}
-11.判断字符串是否为空
public static boolean isEmpty(String str) {
if (null == str || "".equals(str.trim()))
return true;
return false;
}
-12.int数字转为汉语字符串
public static String translateToChinese(int a) {
String[] units = { "", "十", "百", "千", "万", "十", "百", "千", "亿" };
String[] nums = { "一", "二", "三", "四", "五", "六", "七", "八", "九", "十" };
String result = "";
if (a < 0) {
result = "负";
a = Math.abs(a);
}
if (a == 0) {
return "零";
}
String t = String.valueOf(a);
for (int i = t.length() - 1; i >= 0; i--) {
int r = (int) (a / Math.pow(10, i));
if (r % 10 != 0) {
String s = String.valueOf(r);
String l = s.substring(s.length() - 1, s.length());
result += nums[Integer.parseInt(l) - 1];
result += (units[i]);
} else {
if (!result.endsWith("零")) {
result += "零";
}
}
}
String num = a + "";
if (a == 10) {
return "十";
} else if (a > 10 && a < 20) {
return result.substring(1);
} else if (num.endsWith("0")) {
result = result.substring(0, result.length() - 1);
}
return result;
}
-13.String字符反转
public static String strReverse(String string){
if(string==null||string.length()==0)return string;
int length = string.length();
char [] array = string.toCharArray();
for(int i = 0;i<length;i++){
array[i] = string.charAt(length-1-i);
}
return new String(array);
}
-14.输出字符串中存在的数字
import java.util.*;
public class A {
public static void main(String[] args) {
System.out.println(String2Num("giuw984t2n202r3209057292-t283fw"));
}
public static String String2Num(String str) {
if("".equals(str) || str == null)
return "";
str=str.trim();
String str2="";
if(str != null && !"".equals(str)){
for(int i=0;i<str.length();i++){
if(str.charAt(i)>=48 && str.charAt(i)<=57){
str2+=str.charAt(i);
}
}
}
return str2;
}
}
1.
1.
1.
1.
1.
1.
1.
1.
1.
2.
2.
2.
2.
2.
2.
2.
2.
2.
2.
2.
2.
2.
2.
2.
2.
2.
2.
2.
2.
2.
2.
2.
2.
2.
2.
2.
2.
2.
2.
2.
2.
2.
2.
2.
3.
3.
3.
3.
3.
3.
3.
3.
3.
3.
3.
3.
3.
3.
3.
3.
3.
3.
3.
3.
3.