工具类描述:用户字符串操作,这里面包括字符串的decode
、encode
、substract
等等操作
package cn.hgnulb;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 工具类描述:用户字符串操作,这里面包括字符串的decode、encode、substract等等操作
*/
public class StringUtil {
/**
* 把前台传过来的含中文的url字符串转换成标准中文,比如“%E5%8C%97%E4%BA%AC”转换成“北京”
*
* @param url
* url字符串
* @return string
*/
public static String decodeUrl(String url) {
if (url == null)
return "";
String str = "";
try {
str = URLDecoder.decode(url, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return str;
}
/**
* 把比如“北京”转换成“%E5%8C%97%E4%BA%AC”
*
* @param url
* url字符串
* @return string
*/
public static String encodeUrl(String url) {
if (url == null)
return "";
String str = "";
try {
str = URLEncoder.encode(url, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return str;
}
/**
* 取字符串除最后一位的子串,比如 "aaa,bbb,"返回"aaa,bbb",一般用在多个字段进行拼接,要去除最后一位
*
* @param str
* 字符串
* @return string
*/
public static String subTract(String str) {
if (str.length() == 0)
return "";
else
return str.substring(0, str.length() - 1);
}
/**
* 判断字符串是null或"",null或""都返回true
*
* @param str
* 字符串
* @return boolean
*/
public static boolean isNullOrEmpty(String str) {
if (str == null || "".equals(str))
return true;
else
return false;
}
/**
* 判断字符串不是null且"",不是null且""时返回true,否则返回false
*
* @param str
* 字符串
* @return boolean
*/
public static boolean isNotNullOrEmpty(String str) {
if (str != null && !"".equals(str))
return true;
else
return false;
}
/**
* 过滤表情,在移动开发中,有些字符是表情等特殊字符,数据库不识别,需要过滤掉, 替换为*
*
* @param source
* @return string
*/
public static String filterEmoji(String source) {
if (StringUtil.isNotNullOrEmpty(source)) {
return source.replaceAll("[\\ud800\\udc00-\\udbff\\udfff\\ud800-\\udfff]", "*");
} else {
return source;
}
}
/**
* 判断是否是手机号
*
* @param mobile
* @return boolean
*/
public static boolean isMobile(String mobile) {
Pattern p = Pattern.compile("^1[3-9]\\d{9}$");
Matcher m = p.matcher(mobile);
boolean b = m.matches();
return b;
}
public static void main(String[] args) {
System.out.println("StringUtil.decodeUrl(\"%E5%8C%97%E4%BA%AC\"): " + StringUtil.decodeUrl("%E5%8C%97%E4%BA%AC"));
System.out.println("StringUtil.encodeUrl(\"北京\"): " + StringUtil.encodeUrl("北京"));
System.out.println("StringUtil.subTract(\"aaa,bbb,\"): " + StringUtil.subTract("aaa,bbb,"));
System.out.println("StringUtil.isNullOrEmpty(\"\"): " + StringUtil.isNullOrEmpty(""));
System.out.println("StringUtil.isNotNullOrEmpty(\"\"): " + StringUtil.isNotNullOrEmpty(""));
System.out.println("StringUtil.filterEmoji(\"??????\"): " + StringUtil.filterEmoji("??????"));
System.out.println("StringUtil.isMobile(\"15271615292\"): " + StringUtil.isMobile("15271615292"));
}
}
参考答案
StringUtil.decodeUrl("%E5%8C%97%E4%BA%AC"): 北京
StringUtil.encodeUrl("北京"): %E5%8C%97%E4%BA%AC
StringUtil.subTract("aaa,bbb,"): aaa,bbb
StringUtil.isNullOrEmpty(""): true
StringUtil.isNotNullOrEmpty(""): false
StringUtil.filterEmoji("??????"): ******
StringUtil.isMobile("15271615292"): true