【java工具方法】字符串工具类-判空(isEmpty)

工具方法

	/**
	 * 字符串是否为空
	 * null 或 者长度为0
	 * 
	 * @author 静心事成
	 * @param str 字符串
	 * @return boolean
	 * */
	public static boolean isEmpty(CharSequence str) {
		return str == null || str.length() == 0;
	}
	
	/**
	 * 字符串是否不为空
	 * 不为null 并且 长度不为0
	 * 
	 * @author 静心事成
	 * @param str 字符串
	 * @return boolean
	 * */
	public static boolean isNotEmpty(CharSequence str) {
		return false == isEmpty(str);
	}

示例

    public static void main(String[] args) {
        System.out.println(isEmpty(null));
    }

注意

  • 参数使用的CharSequence接口。
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 以下是一个 Java 字符串工具类的示例源代码: ``` public class StringUtils { /** * 字符串是否为 * @param str 待断的字符串 * @return true/false */ public static boolean isEmpty(String str) { return str == null || str.length() == ; } /** * 字符串是否不为 * @param str 待断的字符串 * @return true/false */ public static boolean isNotEmpty(String str) { return !isEmpty(str); } /** * 字符串是否为数字 * @param str 待断的字符串 * @return true/false */ public static boolean isNumeric(String str) { if (isEmpty(str)) { return false; } for (int i = ; i < str.length(); i++) { if (!Character.isDigit(str.charAt(i))) { return false; } } return true; } /** * 字符串是否为字母 * @param str 待断的字符串 * @return true/false */ public static boolean isLetter(String str) { if (isEmpty(str)) { return false; } for (int i = ; i < str.length(); i++) { if (!Character.isLetter(str.charAt(i))) { return false; } } return true; } /** * 字符串是否为汉字 * @param str 待断的字符串 * @return true/false */ public static boolean isChinese(String str) { if (isEmpty(str)) { return false; } for (int i = ; i < str.length(); i++) { if (str.charAt(i) < x4e00 || str.charAt(i) > x9fa5) { return false; } } return true; } /** * 将字符串转换为大写 * @param str 待转换的字符串 * @return 转换后的字符串 */ public static String toUpperCase(String str) { if (isEmpty(str)) { return str; } return str.toUpperCase(); } /** * 将字符串转换为小写 * @param str 待转换的字符串 * @return 转换后的字符串 */ public static String toLowerCase(String str) { if (isEmpty(str)) { return str; } return str.toLowerCase(); } /** * 将字符串首字母大写 * @param str 待转换的字符串 * @return 转换后的字符串 */ public static String capitalize(String str) { if (isEmpty(str)) { return str; } char firstChar = Character.toUpperCase(str.charAt()); if (str.length() == 1) { return String.valueOf(firstChar); } return firstChar + str.substring(1); } /** * 将字符串首字母小写 * @param str 待转换的字符串 * @return 转换后的字符串 */ public static String uncapitalize(String str) { if (isEmpty(str)) { return str; } char firstChar = Character.toLowerCase(str.charAt()); if (str.length() == 1) { return String.valueOf(firstChar); } return firstChar + str.substring(1); } } ``` ### 回答2: Java字符串工具类示例源代码如下: ``` public class StringUtil { /** * 字符串是否为或null * @param str 要断的字符串 * @return 若为或null,返回true;否则返回false */ public static boolean isEmpty(String str) { return str == null || "".equals(str); } /** * 字符串是否为非 * @param str 要断的字符串 * @return 若不为,返回true;否则返回false */ public static boolean isNotEmpty(String str) { return !isEmpty(str); } /** * 去掉字符串两端的白字符 * @param str 要处理的字符串 * @return 去掉白字符的新字符串 */ public static String trim(String str) { return str == null ? null : str.trim(); } /** * 比较两个字符串是否相等,忽略大小写 * @param str1 字符串1 * @param str2 字符串2 * @return 若两个字符串相等,返回true;否则返回false */ public static boolean equalsIgnoreCase(String str1, String str2) { return str1 == null ? str2 == null : str1.equalsIgnoreCase(str2); } /** * 反转字符串 * @param str 要反转的字符串 * @return 反转后的字符串 */ public static String reverse(String str) { return str == null ? null : new StringBuilder(str).reverse().toString(); } } ``` 以上是一个简单的Java字符串工具类示例,包含了常见的字符串操作方法,如字符串是否为、去除白字符、比较字符串是否相等(忽略大小写)、反转字符串等。可以在实际开发中进行改进和扩展,以满足不同的需求。 ### 回答3: 下面是一个简单的Java字符串工具类示例源代码: ```java public class StringUtils { /** * 字符串是否为 * @param str 要断的字符串 * @return true表示字符串,false表示字符串不为 */ public static boolean isEmpty(String str) { return str == null || str.length() == 0; } /** * 反转字符串 * @param str 要反转的字符串 * @return 反转后的字符串 */ public static String reverse(String str) { if (isEmpty(str)) { return str; } StringBuilder sb = new StringBuilder(str); return sb.reverse().toString(); } /** * 将字符串转换为大写 * @param str 要转换的字符串 * @return 转换后的字符串 */ public static String toUpperCase(String str) { if (isEmpty(str)) { return str; } return str.toUpperCase(); } /** * 将字符串转换为小写 * @param str 要转换的字符串 * @return 转换后的字符串 */ public static String toLowerCase(String str) { if (isEmpty(str)) { return str; } return str.toLowerCase(); } } ``` 以上是一个简单的Java字符串工具类示例,其中包含了字符串是否为、反转字符串、将字符串转换为大写和小写等常用方法。你可以根据自己的需求修改或添加其他方法
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值