huTools源码学习记录

sancaiodm Java 2022-08-20 1205 0

【1】大写首字母

public static String upperFirst(CharSequence str) {

if (null == str) {

return null;

}

if (str.length() > 0) {

char firstChar = str.charAt(0);

if (Character.isLowerCase(firstChar)) {

return Character.toUpperCase(firstChar) + subSuf(str, 1);

}

}

return str.toString();

}

【2】小写首字母

public static String lowerFirst(CharSequence str) {

if (null == str) {

return null;

}

if (str.length() > 0) {

char firstChar = str.charAt(0);

if (Character.isUpperCase(firstChar)) {

return Character.toLowerCase(firstChar) + subSuf(str, 1);

}

}

return str.toString();

}

【3】 过滤字符串

public static String filter(CharSequence str, final Filter<Character> filter) {

if (str == null || filter == null) {

return null == cs ? null : cs.toString();

}

int len = str.length();

final StringBuilder sb = new StringBuilder(len);

char c;

for (int i = 0; i < len; i++) {

c = str.charAt(i);

if (filter.accept(c)) {

sb.append(c);

}

}

return sb.toString();

}


评论