StringUtils

[align=center][size=large]StringUtils[/size][/align]



package com.java.study.test.unit7;

/**
* String常用工具类
*/
public class StringUtils {

/**
* 为空
* @param str
* @return
*/
public static boolean isEmpty(String str) {
return str == null || str.length() == 0;
}

/**
* 非空
* @param str
* @return
*/
public static boolean isNotEmpty(String str) {
return !isEmpty(str);
}

/**
* 为空,判断空字符
* @param str
* @return
*/
public static boolean isBlank(String str) {
int strLen;
if (str == null || (strLen = str.length()) == 0)
return true;
for (int i = 0; i < strLen; i++)
if (!Character.isWhitespace(str.charAt(i)))
return false;

return true;
}

public static boolean isNotBlank(String str) {
return !isBlank(str);
}

public static String trim(String str) {
return str != null ? str.trim() : null;
}

public static String trimToNull(String str) {
String ts = trim(str);
return isEmpty(ts) ? null : ts;
}

public static String trimToEmpty(String str) {
return str != null ? str.trim() : "";
}

public static String strip(String str) {
return strip(str, null);
}

public static String stripToNull(String str) {
if (str == null) {
return null;
} else {
str = strip(str, null);
return str.length() != 0 ? str : null;
}
}

public static String stripToEmpty(String str) {
return str != null ? strip(str, null) : "";
}

public static String strip(String str, String stripChars) {
if (isEmpty(str)) {
return str;
} else {
str = stripStart(str, stripChars);
return stripEnd(str, stripChars);
}
}

public static String stripStart(String str, String stripChars) {
int strLen;
if (str == null || (strLen = str.length()) == 0)
return str;
int start = 0;
if (stripChars == null) {
for (; start != strLen && Character.isWhitespace(str.charAt(start)); start++)
;
} else {
if (stripChars.length() == 0)
return str;
for (; start != strLen
&& stripChars.indexOf(str.charAt(start)) != -1; start++)
;
}
return str.substring(start);
}

public static String stripEnd(String str, String stripChars) {
int end;
if (str == null || (end = str.length()) == 0)
return str;
if (stripChars == null) {
for (; end != 0 && Character.isWhitespace(str.charAt(end - 1)); end--)
;
} else {
if (stripChars.length() == 0)
return str;
for (; end != 0 && stripChars.indexOf(str.charAt(end - 1)) != -1; end--)
;
}
return str.substring(0, end);
}

public static String[] stripAll(String strs[]) {
return stripAll(strs, null);
}

public static String[] stripAll(String strs[], String stripChars) {
int strsLen;
if (strs == null || (strsLen = strs.length) == 0)
return strs;
String newArr[] = new String[strsLen];
for (int i = 0; i < strsLen; i++)
newArr[i] = strip(strs[i], stripChars);

return newArr;
}

public static boolean equals(String str1, String str2) {
return str1 != null ? str1.equals(str2) : str2 == null;
}

public static boolean equalsIgnoreCase(String str1, String str2) {
return str1 != null ? str1.equalsIgnoreCase(str2) : str2 == null;
}

public static int indexOf(String str, char searchChar) {
if (isEmpty(str))
return -1;
else
return str.indexOf(searchChar);
}

public static int indexOf(String str, char searchChar, int startPos) {
if (isEmpty(str))
return -1;
else
return str.indexOf(searchChar, startPos);
}

public static int indexOf(String str, String searchStr) {
if (str == null || searchStr == null)
return -1;
else
return str.indexOf(searchStr);
}

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值