获取带圈字母 /封闭式字母 ,从a-z or A-Z

This commit is contained in:
duandazhi 2021-03-26 19:19:42 +08:00
parent 739af1befe
commit 80e54e512f

View File

@ -1,5 +1,7 @@
package cn.hutool.core.text; package cn.hutool.core.text;
import java.math.BigInteger;
/** /**
* 符号工具类 * 符号工具类
* @author dazer & neusoft * @author dazer & neusoft
@ -51,18 +53,48 @@ public class SpecialSymbolUtil {
* @param number 十进制数字从1-->1011--->20 * @param number 十进制数字从1-->1011--->20
* @return * @return
*/ */
public static String enclosedAlphanumericsCircleByInt(int number) { public static String enclosedNumericsByInt(int number) {
if (number <= 0 || number > 20) { if (number <= 0 || number > 20) {
throw new IllegalArgumentException("number取值范围是[1-20]的正整数,包含1和20"); throw new IllegalArgumentException("number取值范围是[1-20]的正整数,包含1和20");
} }
String start = ""; String start = "";
String unicodeStart = UnicodeUtil.toUnicode(start).substring(UNICODE_START_CHAR.length(), String unicodeStart = UnicodeUtil.toUnicode(start).substring(UNICODE_START_CHAR.length(),
UnicodeUtil.toUnicode(start).length() - 1); UnicodeUtil.toUnicode(start).length() - 1);
String beginHex = unicodeStart.substring(0, 2); // begin: U+246x的24 // begin: U+246x的24
String middleHex = number >= 17 ? "7" : "6"; // U+246x的6 String beginHex = unicodeStart.substring(0, 2);
String endHex = Integer.toHexString(number >= 17 ? number - 17 : number - 1); // A // U+246x的6
String middleHex = number >= 17 ? "7" : "6";
// A
String endHex = Integer.toHexString(number >= 17 ? number - 17 : number - 1);
// U + 24 + 60 // U + 24 + 60
String unicodeStr = UNICODE_START_CHAR + beginHex + middleHex + endHex; String unicodeStr = UNICODE_START_CHAR + beginHex + middleHex + endHex;
return UnicodeUtil.toString(unicodeStr); return UnicodeUtil.toString(unicodeStr);
} }
/**
* 获取带圈字母 /封闭式字母 从a-z or A-Z
* 根据字符 获取
* <code>
* System.out.println(encloseAlphabetByChar( 'A'));
* System.out.println(encloseAlphabetByChar( 'a'));
* System.out.println(encloseAlphabetByChar( 'z'));
* System.out.println(encloseAlphabetByChar( 'Z'))
* </code>
* @author dazer
* @param letter 字母不区分大小写'a''b''c''d'...'x''y''z'; 'A''B'...'Z'
* @date 2021/3/26 18:10
*/
public static String encloseAlphabetByChar(char letter) {
if (!(letter >= 'a' && letter <= 'z' || letter >= 'A' && letter <= 'Z')) {
throw new IllegalArgumentException("number取值范围是[a-z]、[A-Z]的字符");
}
// \u24b6
String start = "";
String hexStr = UnicodeUtil.toUnicode(start).substring(UNICODE_START_CHAR.length());
int difference = letter >= 'a' && letter <= 'z' ? (letter - (int)'a') : (letter - (int)'A');
String hex = new BigInteger(hexStr, 16).add(new BigInteger(String.valueOf(difference), 10)).toString(16);
//
String unicodeStr = UNICODE_START_CHAR + hex;
return UnicodeUtil.toString(unicodeStr);
}
} }