/**
*
*/
package cn.xdl.utils.common;
import java.util.Random;
/**
* @author liurui
* @date 2019年8月27日
*/
public class EasyStrUtil {
public static boolean isEmpty(String str){
if(str == null || "".equals(str)) return true;
if("".equals(str.trim())) return true;
return false;
}
public static boolean isNotEmpty(String str){
return !isEmpty(str);
}
/**
* 判断字符串中是否包含空格
* @param str 目标字符串
* @return
*/
public static boolean hasBlank(String str) {
if(isEmpty(str)) return true;
return str.contains(" ");
}
/**
* 去掉字符串中所有空格,去掉前后空格可以使用trim()
* @param str 目标字符串
* @return
*/
public static String removeBlank(String str) {
if(isEmpty(str)) return null;
return str.replace(" ", "");
}
/**
* 拼串
* @param strings
* @return
*/
public static String mergeStr(String...strings) {
StringBuilder sb = new StringBuilder();
for (String str : strings) {
sb.append(str);
}
return sb.toString();
}
/**
* 获取指定个数数字字符串
* @param num 个数
* @return
*/
public static String getRanNum(Integer num) {
String str = "";
for(int i=0;i<num;i++) {
str += (int)(Math.random()*10);
}
return str;
}
/**
* 获取指定个数字符串
* @param num 个数
* @return
*/
public static String getRanStr(Integer num) {
String str="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
Random random = new Random();
String res = "";
for(int i=0;i<num;i++) {
int ranNum = random.nextInt(str.length());
res += str.charAt(ranNum);
}
return res;
}
public static void main(String[] args) {
System.out.println(getRanStr(4));
}
}
Java字符串工具类
最新推荐文章于 2024-08-15 16:46:51 发布
本文介绍了一个实用的字符串操作工具类,包括判断字符串是否为空、是否包含空格、去除空格、字符串拼接及生成随机字符串等功能。这些方法能够简化日常开发中常见的字符串处理任务,提高编程效率。
1113

被折叠的 条评论
为什么被折叠?



