java字符串工具类

一、java字符串工具类

package 包路径;
import org.apache.commons.lang.StringUtils;

public class StringUtil {
/**
* 字符串隐藏部分内容方法

* @param str
*            待处理字符串
* @param start
*            前面保留长度
* @param end
*            末尾保留长度
* @return 处理过的字符串
*/
public static String formatStringByRange(String str, int start, int end) {
if (StringUtils.isEmpty(str) || str.length() <= start + end) {
return str;
}
int startNum = str.length() - start - end;
StringBuffer sb = new StringBuffer();
for (int i = 0; i < str.length(); i++) {
if (i >= start && i < start + startNum) {
sb.append("*");
continue;
}
sb.append(str.charAt(i));
}
return sb.toString();
}


/**
* 判断对象是否为空
*/
public static Boolean isBlank(Object obj) {
if (null == obj || StringUtils.isBlank(obj.toString())) {
return true;
}
return false;
}


/**
* 判断对象是否不为空
*/
public static Boolean isNotBlank(Object obj) {
if (isBlank(obj)) {
return false;
}
return true;
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值