字符串相关工具类
来源:互联网 发布:安康市 软件正版 编辑:程序博客网 时间:2024/06/06 01:16
public final class StringUtils {
private StringUtils() {
throw new UnsupportedOperationException("u can't instantiate me...");
}
/**
* 判断字符串是否为null或长度为0
*
* @param s 待校验字符串
* @return {@code true}: 空<br> {@code false}: 不为空
*/
public static boolean isEmpty(CharSequence s) {
return s == null || s.length() == 0;
}
/**
* 判断字符串是否为null或全为空格
*
* @param s 待校验字符串
* @return {@code true}: null或全空格<br> {@code false}: 不为null且不全空格
*/
public static boolean isTrimEmpty(String s) {
return (s == null || s.trim().length() == 0);
}
/**
* 判断字符串是否为null或全为空白字符
*
* @param s 待校验字符串
* @return {@code true}: null或全空白字符<br> {@code false}: 不为null且不全空白字符
*/
public static boolean isSpace(String s) {
if (s == null) return true;
for (int i = 0, len = s.length(); i < len; ++i) {
if (!Character.isWhitespace(s.charAt(i))) {
return false;
}
}
return true;
}
/**
* 判断两字符串是否相等
*
* @param a 待校验字符串a
* @param b 待校验字符串b
* @return {@code true}: 相等<br>{@code false}: 不相等
*/
public static boolean equals(CharSequence a, CharSequence b) {
if (a == b) return true;
int length;
if (a != null && b != null && (length = a.length()) == b.length()) {
if (a instanceof String && b instanceof String) {
return a.equals(b);
} else {
for (int i = 0; i < length; i++) {
if (a.charAt(i) != b.charAt(i)) return false;
}
return true;
}
}
return false;
}
/**
* 判断两字符串忽略大小写是否相等
*
* @param a 待校验字符串a
* @param b 待校验字符串b
* @return {@code true}: 相等<br>{@code false}: 不相等
*/
public static boolean equalsIgnoreCase(String a, String b) {
return a == null ? b == null : a.equalsIgnoreCase(b);
}
/**
* null转为长度为0的字符串
*
* @param s 待转字符串
* @return s为null转为长度为0字符串,否则不改变
*/
public static String null2Length0(String s) {
return s == null ? "" : s;
}
/**
* 返回字符串长度
*
* @param s 字符串
* @return null返回0,其他返回自身长度
*/
public static int length(CharSequence s) {
return s == null ? 0 : s.length();
}
/**
* 首字母大写
*
* @param s 待转字符串
* @return 首字母大写字符串
*/
public static String upperFirstLetter(String s) {
if (isEmpty(s) || !Character.isLowerCase(s.charAt(0))) return s;
return String.valueOf((char) (s.charAt(0) - 32)) + s.substring(1);
}
/**
* 首字母小写
*
* @param s 待转字符串
* @return 首字母小写字符串
*/
public static String lowerFirstLetter(String s) {
if (isEmpty(s) || !Character.isUpperCase(s.charAt(0))) return s;
return String.valueOf((char) (s.charAt(0) + 32)) + s.substring(1);
}
/**
* 反转字符串
*
* @param s 待反转字符串
* @return 反转字符串
*/
public static String reverse(String s) {
int len = length(s);
if (len <= 1) return s;
int mid = len >> 1;
char[] chars = s.toCharArray();
char c;
for (int i = 0; i < mid; ++i) {
c = chars[i];
chars[i] = chars[len - i - 1];
chars[len - i - 1] = c;
}
return new String(chars);
}
/**
* 转化为半角字符
*
* @param s 待转字符串
* @return 半角字符串
*/
public static String toDBC(String s) {
if (isEmpty(s)) return s;
char[] chars = s.toCharArray();
for (int i = 0, len = chars.length; i < len; i++) {
if (chars[i] == 12288) {
chars[i] = ' ';
} else if (65281 <= chars[i] && chars[i] <= 65374) {
chars[i] = (char) (chars[i] - 65248);
} else {
chars[i] = chars[i];
}
}
return new String(chars);
}
/**
* 转化为全角字符
*
* @param s 待转字符串
* @return 全角字符串
*/
public static String toSBC(String s) {
if (isEmpty(s)) return s;
char[] chars = s.toCharArray();
for (int i = 0, len = chars.length; i < len; i++) {
if (chars[i] == ' ') {
chars[i] = (char) 12288;
} else if (33 <= chars[i] && chars[i] <= 126) {
chars[i] = (char) (chars[i] + 65248);
} else {
chars[i] = chars[i];
}
}
return new String(chars);
}
}
private StringUtils() {
throw new UnsupportedOperationException("u can't instantiate me...");
}
/**
* 判断字符串是否为null或长度为0
*
* @param s 待校验字符串
* @return {@code true}: 空<br> {@code false}: 不为空
*/
public static boolean isEmpty(CharSequence s) {
return s == null || s.length() == 0;
}
/**
* 判断字符串是否为null或全为空格
*
* @param s 待校验字符串
* @return {@code true}: null或全空格<br> {@code false}: 不为null且不全空格
*/
public static boolean isTrimEmpty(String s) {
return (s == null || s.trim().length() == 0);
}
/**
* 判断字符串是否为null或全为空白字符
*
* @param s 待校验字符串
* @return {@code true}: null或全空白字符<br> {@code false}: 不为null且不全空白字符
*/
public static boolean isSpace(String s) {
if (s == null) return true;
for (int i = 0, len = s.length(); i < len; ++i) {
if (!Character.isWhitespace(s.charAt(i))) {
return false;
}
}
return true;
}
/**
* 判断两字符串是否相等
*
* @param a 待校验字符串a
* @param b 待校验字符串b
* @return {@code true}: 相等<br>{@code false}: 不相等
*/
public static boolean equals(CharSequence a, CharSequence b) {
if (a == b) return true;
int length;
if (a != null && b != null && (length = a.length()) == b.length()) {
if (a instanceof String && b instanceof String) {
return a.equals(b);
} else {
for (int i = 0; i < length; i++) {
if (a.charAt(i) != b.charAt(i)) return false;
}
return true;
}
}
return false;
}
/**
* 判断两字符串忽略大小写是否相等
*
* @param a 待校验字符串a
* @param b 待校验字符串b
* @return {@code true}: 相等<br>{@code false}: 不相等
*/
public static boolean equalsIgnoreCase(String a, String b) {
return a == null ? b == null : a.equalsIgnoreCase(b);
}
/**
* null转为长度为0的字符串
*
* @param s 待转字符串
* @return s为null转为长度为0字符串,否则不改变
*/
public static String null2Length0(String s) {
return s == null ? "" : s;
}
/**
* 返回字符串长度
*
* @param s 字符串
* @return null返回0,其他返回自身长度
*/
public static int length(CharSequence s) {
return s == null ? 0 : s.length();
}
/**
* 首字母大写
*
* @param s 待转字符串
* @return 首字母大写字符串
*/
public static String upperFirstLetter(String s) {
if (isEmpty(s) || !Character.isLowerCase(s.charAt(0))) return s;
return String.valueOf((char) (s.charAt(0) - 32)) + s.substring(1);
}
/**
* 首字母小写
*
* @param s 待转字符串
* @return 首字母小写字符串
*/
public static String lowerFirstLetter(String s) {
if (isEmpty(s) || !Character.isUpperCase(s.charAt(0))) return s;
return String.valueOf((char) (s.charAt(0) + 32)) + s.substring(1);
}
/**
* 反转字符串
*
* @param s 待反转字符串
* @return 反转字符串
*/
public static String reverse(String s) {
int len = length(s);
if (len <= 1) return s;
int mid = len >> 1;
char[] chars = s.toCharArray();
char c;
for (int i = 0; i < mid; ++i) {
c = chars[i];
chars[i] = chars[len - i - 1];
chars[len - i - 1] = c;
}
return new String(chars);
}
/**
* 转化为半角字符
*
* @param s 待转字符串
* @return 半角字符串
*/
public static String toDBC(String s) {
if (isEmpty(s)) return s;
char[] chars = s.toCharArray();
for (int i = 0, len = chars.length; i < len; i++) {
if (chars[i] == 12288) {
chars[i] = ' ';
} else if (65281 <= chars[i] && chars[i] <= 65374) {
chars[i] = (char) (chars[i] - 65248);
} else {
chars[i] = chars[i];
}
}
return new String(chars);
}
/**
* 转化为全角字符
*
* @param s 待转字符串
* @return 全角字符串
*/
public static String toSBC(String s) {
if (isEmpty(s)) return s;
char[] chars = s.toCharArray();
for (int i = 0, len = chars.length; i < len; i++) {
if (chars[i] == ' ') {
chars[i] = (char) 12288;
} else if (33 <= chars[i] && chars[i] <= 126) {
chars[i] = (char) (chars[i] + 65248);
} else {
chars[i] = chars[i];
}
}
return new String(chars);
}
}
阅读全文
0 0
- 字符串相关工具类
- 字符串相关工具类
- Android StringUtils-字符串相关工具类
- Android工具类之字符串工具类,提供一些字符串相关的便捷方法
- Spring 相关工具类
- Web相关工具类
- 文件相关工具类
- 安全相关工具类
- Math工具类相关
- 尺寸相关工具类
- 文件相关工具类
- Json相关工具类
- 权限相关工具类
- 屏幕相关工具类
- 阴历相关工具类
- Log相关工具类
- 定位相关工具类
- 键盘相关工具类
- 设计模式之原型模式的学习思考
- STL allocator 类探究
- _itoa_s出错的问题
- JavaMail实现邮箱验证码
- openstack 常见问题及解决方法
- 字符串相关工具类
- Ubuntu16.04安装后要做的一些事情
- 2、STM32基本接口驱动--IO 单总线 串口 SPI IIC
- 【HTML5入门】Jquery中获取控件值的三种方法
- Jquery.DataTable使用
- SDUT 1209 求实数绝对值
- 洛谷P3375 【模板】KMP字符串匹配
- grep
- 浏览器Web开发者工具 ------ 控制台内管理cookie,indexedDB,localStorage,sessionStorage