判断字符串是否为空、连接字符串数组、反转字符串、判断是否为数字、将字符串首字母大写、转换为驼峰式命名、截取字符串、判断是否为IP地址、替换字符串中的指定内容、判断是否为有效的邮箱地址、判断是否为有效的手机号码、将字符串数组转换为大写、统计字符串中某个字符出现的次数、判断字符串是否为有效的URL地址、判断字符串是否为有效的日期格式、判断字符串是否为有效的身份证号码、获取字符串中的所有数字、判断字符串是否为回文字符串、格式化字符串
import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MyStringUtils {
/**
* 判断字符串是否为空(包括null、空字符串、只包含空格的字符串)
* @param str 要判断的字符串
* @return 是否为空
*/
public static boolean isEmpty(String str) {
return str == null || str.trim().isEmpty();
}
/**
* 使用指定分隔符将字符串数组连接成一个字符串
* @param strings 字符串数组
* @param delimiter 分隔符
* @return 连接后的字符串
*/
public static String join(String[] strings, String delimiter) {
return String.join(delimiter, strings);
}
/**
* 反转字符串
* @param str 要反转的字符串
* @return 反转后的字符串
*/
public static String reverse(String str) {
return new StringBuilder(str).reverse().toString();
}
/**
* 判断字符串是否为数字
* @param str 要判断的字符串
* @return 是否为数字
*/
public static boolean isNumeric(String str) {
if (isEmpty(str)) {
return false;
}
Pattern pattern = Pattern.compile("[0-9]*");
return pattern.matcher(str).matches();
}
/**
* 将首字母转换为大写
* @param str 要转换的字符串
* @return 转换后的字符串
*/
public static String capitalize(String str) {
if (isEmpty(str)) {
return str;
}
return Character.toUpperCase(str.charAt(0)) + str.substring(1);
}
/**
* 将字符串转换为驼峰式命名
* @param str 要转换的字符串
* @return 转换后的字符串
*/
public static String toCamelCase(String str) {
if (isEmpty(str)) {
return str;
}
StringBuilder sb = new StringBuilder();
String[] parts = str.split("_|-|\\.");
for (String part : parts) {
if (!isEmpty(part)) {
sb.append(capitalize(part.toLowerCase()));
}
}
return sb.toString();
}
/**
* 截取字符串的一部分
* @param str 要截取的字符串
* @param beginIndex 起始索引
* @param endIndex 结束索引
* @return 截取后的字符串
*/
public static String substring(String str, int beginIndex, int endIndex) {
if (isEmpty(str)) {
return str;
}
if (beginIndex < 0) {
beginIndex = 0;
}
if (endIndex > str.length()) {
endIndex = str.length();
}
if (beginIndex >= endIndex) {
return "";
}
return str.substring(beginIndex, endIndex);
}
/**
* 判断字符串是否为IP地址
* @param str 要判断的字符串
* @return 是否为IP地址
*/
public static boolean isIPAddress(String str) {
if (isEmpty(str)) {
return false;
}
Pattern pattern = Pattern.compile("^(\\d{1,3}\\.){3}\\d{1,3}$");
return pattern.matcher(str).matches();
}
/**
* 将字符串转换为驼峰式命名,并将分隔符去除
* @param str 要转换的字符串
* @param delimiter 分隔符
* @return 转换后的字符串
*/
public static String toCamelCaseWithoutDelimiter(String str, String delimiter) {
if (isEmpty(str)) {
return str;
}
StringBuilder sb = new StringBuilder();
String[] parts = str.split(Pattern.quote(delimiter));
for (String part : parts) {
if (!isEmpty(part)) {
sb.append(capitalize(part.toLowerCase()));
}
}
return sb.toString();
}
/**
* 判断字符串是否为有效的邮箱地址
* @param str 要判断的字符串
* @return 是否为有效的邮箱地址
*/
public static boolean isValidEmail(String str) {
if (isEmpty(str)) {
return false;
}
Pattern pattern = Pattern.compile("^[\\w.-]+@[\\w.-]+\\.[a-zA-Z]{2,}$");
return pattern.matcher(str).matches();
}
/**
* 判断字符串是否为有效的手机号码
* @param str 要判断的字符串
* @return 是否为有效的手机号码
*/
public static boolean isValidPhoneNumber(String str) {
if (isEmpty(str)) {
return false;
}
Pattern pattern = Pattern.compile("^1[0-9]{10}$");
return pattern.matcher(str).matches();
}
/**
* 将字符串数组转换为大写
* @param strings 字符串数组
* @return 转换后的字符串数组
*/
public static String[] toUpperCase(String[] strings) {
if (strings == null) {
return null;
}
return Arrays.stream(strings)
.map(String::toUpperCase)
.toArray(String[]::new);
}
/**
* 统计字符串中某个字符出现的次数
* @param str 要统计的字符串
* @param c 要统计的字符
* @return 字符出现的次数
*/
public static int countOccurrences(String str, char c) {
if (isEmpty(str)) {
return 0;
}
int count = 0;
for (char ch : str.toCharArray()) {
if (ch == c) {
count++;
}
}
return count;
}
/**
* 替换字符串中的指定内容
* @param str 要替换的字符串
* @param search 要搜索的字符串
* @param replace 要替换的字符串
* @return 替换后的字符串
*/
public static String replace(String str, String search, String replace) {
if (isEmpty(str) || isEmpty(search)) {
return str;
}
return str.replace(search, replace);
}
/**
* 判断字符串是否为有效的URL地址
* @param str 要判断的字符串
* @return 是否为有效的URL地址
*/
public static boolean isValidURL(String str) {
if (isEmpty(str)) {
return false;
}
Pattern pattern = Pattern.compile("^((https?|ftp|file)://)?([\\w-]+(\\.[\\w-]+)*"
+ "(:(0-9)*)?)((/(\\w-]+(\\.[\\w-]+)*)+)?/?(\\?[\\w\\-.,@?^=%&:/~+#]*"
+ "[\\w\\-@?^=%&/~+#])?)?$");
return pattern.matcher(str).matches();
}
/**
* 判断字符串是否为有效的日期格式(yyyy-MM-dd)
* @param str 要判断的字符串
* @return 是否为有效的日期格式
*/
public static boolean isValidDate(String str) {
if (isEmpty(str)) {
return false;
}
Pattern pattern = Pattern.compile("^\\d{4}-\\d{2}-\\d{2}$");
Matcher matcher = pattern.matcher(str);
if (!matcher.matches()) {
return false;
}
matcher.reset();
if (matcher.find()) {
String year = matcher.group(1);
String month = matcher.group(2);
String day = matcher.group(3);
if ("2".equals(month) && "29".equals(day)) {
int y = Integer.parseInt(year);
return (y % 4 == 0 && y % 100 != 0) || (y % 400 == 0);
}
int m = Integer.parseInt(month);
int d = Integer.parseInt(day);
return m >= 1 && m <= 12 && d >= 1 && d <= 31;
}
return false;
}
/**
* 判断字符串是否为有效的身份证号码(支持15位和18位)
* @param str 要判断的字符串
* @return 是否为有效的身份证号码
*/
public static boolean isValidIDCard(String str) {
if (isEmpty(str)) {
return false;
}
Pattern pattern = Pattern.compile("^[1-9]\\d{5}(18|19|20)?\\d{2}"
+ "(0[1-9]|10|11|12)(0[1-9]|[1-2]\\d|30|31)"
+ "\\d{3}[\\dxX]?$");
return pattern.matcher(str).matches();
}
/**
* 获取字符串中的所有数字
* @param str 要提取的字符串
* @return 提取后的数字字符串
*/
public static String extractNumbers(String str) {
if (isEmpty(str)) {
return "";
}
StringBuilder sb = new StringBuilder();
for (char c : str.toCharArray()) {
if (Character.isDigit(c)) {
sb.append(c);
}
}
return sb.toString();
}
/**
* 判断字符串是否为回文字符串
* @param str 要判断的字符串
* @return 是否为回文字符串
*/
public static boolean isPalindrome(String str) {
if (isEmpty(str)) {
return false;
}
String reverse = reverse(str);
return str.equals(reverse);
}
/**
* 格式化字符串,替换占位符中的参数
* @param pattern 字符串模板
* @param args 替换参数
* @return 格式化后的字符串
*/
public static String formatString(String pattern, Object... args) {
return String.format(pattern, args);
}
}
字符串格式化替换时 %s
代表替换的是字符串 %d 代表替换的是整数 %
.2f 代表替换的是浮点数的精度为两位小数