com.iohao.game.common.kit.StrKit Maven / Gradle / Ivy
/*
* ioGame
* Copyright (C) 2021 - present 渔民小镇 ([email protected]、[email protected]) . All Rights Reserved.
* # iohao.com . 渔民小镇
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see .
*/
package com.iohao.game.common.kit;
import com.iohao.game.common.kit.adapter.AdapterHuUtils;
import lombok.NonNull;
import lombok.experimental.UtilityClass;
import java.util.Map;
/**
* @author 渔民小镇
* @date 2022-05-28
*/
@UtilityClass
public class StrKit {
/**
* 首字母转换为大写
*
* @param value 字符串
* @return 转换后的字符串
*/
public String firstCharToUpperCase(String value) {
char firstChar = value.charAt(0);
if (firstChar >= 'a' && firstChar <= 'z') {
char[] arr = value.toCharArray();
arr[0] -= 32;
return String.valueOf(arr);
}
return value;
}
public String format(@NonNull CharSequence template, @NonNull Map, ?> map) {
return AdapterHuUtils.format(template, map);
}
public String format(@NonNull CharSequence template, Object... params) {
return AdapterHuUtils.format(template, params);
}
/**
* 字符串是否为空,空的定义如下:
*
* - {@code null}
* - 空字符串:{@code ""}
*
*
* 例:
*
* - {@code StrKit.isEmpty(null) // true}
* - {@code StrKit.isEmpty("") // true}
* - {@code StrKit.isEmpty(" \t\n") // false}
* - {@code StrKit.isEmpty("abc") // false}
*
*
* 建议:
*
* - 该方法建议用于工具类或任何可以预期的方法参数的校验中。
*
*
* @param str 被检测的字符串
* @return 是否为空
*/
public boolean isEmpty(String str) {
return str == null || str.isEmpty() || str.isBlank();
}
public boolean isEmpty(CharSequence str) {
return str == null || str.isEmpty();
}
/**
* 字符串是否为非空白,非空白的定义如下:
*
* - 不为 {@code null}
* - 不为空字符串:{@code ""}
*
*
* 例:
*
* - {@code StrKit.isNotEmpty(null) // false}
* - {@code StrKit.isNotEmpty("") // false}
* - {@code StrKit.isNotEmpty(" \t\n") // true}
* - {@code StrKit.isNotEmpty("abc") // true}
*
*
* 建议:该方法建议用于工具类或任何可以预期的方法参数的校验中。
*
* @param str 被检测的字符串
* @return 是否为非空
*/
public boolean isNotEmpty(String str) {
return !isEmpty(str);
}
public boolean isBlank(CharSequence cs) {
int strLen;
if (cs == null || (strLen = cs.length()) == 0) {
return true;
}
for (int i = 0; i < strLen; i++) {
if (!Character.isWhitespace(cs.charAt(i))) {
return false;
}
}
return true;
}
public boolean isNotBlank(CharSequence cs) {
return !isBlank(cs);
}
}