当前位置:网站首页>String.toLowerCase(Locale.ROOT)
String.toLowerCase(Locale.ROOT)
2022-08-09 06:22:00 【考拉盖饭】
String.toLowerCase(Locale.ROOT)
环境:jdk1.8
Locale是针对区域的转换规则
首先看到Locale类中的ROOT是用两个空字符串初始化的对象,表示所有语言环境的基本语言环境,并且用于语言/国家无关的区域设置,用于区域设置敏感的操作。
/** * Useful constant for the root locale. The root locale is the locale whose * language, country, and variant are empty ("") strings. This is regarded * as the base locale of all locales, and is used as the language/country * neutral locale for the locale sensitive operations. * * @since 1.6 */
static public final Locale ROOT = createConstant("", "");
部分语种和区域属性
/** Useful constant for language. */
static public final Locale JAPANESE = createConstant("ja", "");
/** Useful constant for language. */
static public final Locale KOREAN = createConstant("ko", "");
/** Useful constant for language. */
static public final Locale CHINESE = createConstant("zh", "");
/** Useful constant for language. */
static public final Locale SIMPLIFIED_CHINESE = createConstant("zh", "CN"); // 中国简体
/** Useful constant for language. */
static public final Locale TRADITIONAL_CHINESE = createConstant("zh", "TW"); // 中国繁体
/** Useful constant for country. */
static public final Locale FRANCE = createConstant("fr", "FR");
/** Useful constant for country. */
static public final Locale GERMANY = createConstant("de", "DE");
/** Useful constant for country. */
static public final Locale ITALY = createConstant("it", "IT");
/** Useful constant for country. */
static public final Locale JAPAN = createConstant("ja", "JP");
/** Useful constant for country. */
static public final Locale KOREA = createConstant("ko", "KR");
来看看String转小写的无参方法
public String toLowerCase() {
return toLowerCase(Locale.getDefault()); // 最终调用的都是有参转小写
}
// 这里调用Locale的initDefault获取Locale
private static Locale initDefault() {
String language, region, script, country, variant;
language = AccessController.doPrivileged(
new GetPropertyAction("user.language", "en")); // 获取主机的语种
// for compatibility, check for old user.region property
region = AccessController.doPrivileged(
new GetPropertyAction("user.region")); // 获取主机的区域
if (region != null) {
// region can be of form country, country_variant, or _variant
int i = region.indexOf('_');
if (i >= 0) {
country = region.substring(0, i);
variant = region.substring(i + 1);
} else {
country = region;
variant = "";
}
script = "";
} else {
script = AccessController.doPrivileged(
new GetPropertyAction("user.script", ""));
country = AccessController.doPrivileged(
new GetPropertyAction("user.country", ""));
variant = AccessController.doPrivileged(
new GetPropertyAction("user.variant", ""));
}
return getInstance(language, script, country, variant, null);
}
上面的获取语种和区域,在中国大陆大部分是 -Duser.language=“zh” \ -Duser.region=“CN” \
也就是对应的
static public final Locale SIMPLIFIED_CHINESE = createConstant("zh", "CN");
来看看String转小写的最终方法:
public String toLowerCase(Locale locale) {
if (locale == null) {
throw new NullPointerException();
}
int firstUpper;
final int len = value.length;
/* Now check if there are any characters that need to be changed. */
scan: {
for (firstUpper = 0 ; firstUpper < len; ) {
char c = value[firstUpper];
if ((c >= Character.MIN_HIGH_SURROGATE)
&& (c <= Character.MAX_HIGH_SURROGATE)) {
int supplChar = codePointAt(firstUpper);
if (supplChar != Character.toLowerCase(supplChar)) {
break scan;
}
firstUpper += Character.charCount(supplChar);
} else {
if (c != Character.toLowerCase(c)) {
break scan;
}
firstUpper++;
}
}
return this;
}
char[] result = new char[len];
int resultOffset = 0; /* result may grow, so i+resultOffset * is the write location in result */
/* Just copy the first few lowerCase characters. */
System.arraycopy(value, 0, result, 0, firstUpper);
String lang = locale.getLanguage();
boolean localeDependent =
(lang == "tr" || lang == "az" || lang == "lt");
char[] lowerCharArray;
int lowerChar;
int srcChar;
int srcCount;
for (int i = firstUpper; i < len; i += srcCount) {
srcChar = (int)value[i];
if ((char)srcChar >= Character.MIN_HIGH_SURROGATE
&& (char)srcChar <= Character.MAX_HIGH_SURROGATE) {
srcChar = codePointAt(i);
srcCount = Character.charCount(srcChar);
} else {
srcCount = 1;
}
if (localeDependent ||
srcChar == '\u03A3' || // 希腊大写字母
srcChar == '\u0130') {
// 拉丁文大写字母I上面带点
lowerChar = ConditionalSpecialCasing.toLowerCaseEx(this, i, locale);
} else {
lowerChar = Character.toLowerCase(srcChar);
}
if ((lowerChar == Character.ERROR)
|| (lowerChar >= Character.MIN_SUPPLEMENTARY_CODE_POINT)) {
if (lowerChar == Character.ERROR) {
lowerCharArray =
ConditionalSpecialCasing.toLowerCaseCharArray(this, i, locale);
} else if (srcCount == 2) {
resultOffset += Character.toChars(lowerChar, result, i + resultOffset) - srcCount;
continue;
} else {
lowerCharArray = Character.toChars(lowerChar);
}
/* Grow result if needed */
int mapLen = lowerCharArray.length;
if (mapLen > srcCount) {
char[] result2 = new char[result.length + mapLen - srcCount];
System.arraycopy(result, 0, result2, 0, i + resultOffset);
result = result2;
}
for (int x = 0; x < mapLen; ++x) {
result[i + resultOffset + x] = lowerCharArray[x];
}
resultOffset += (mapLen - srcCount);
} else {
result[i + resultOffset] = (char)lowerChar;
}
}
return new String(result, 0, len + resultOffset);
}
从上面toLowerCase方法中
boolean localeDependent =
(lang == "tr" || lang == "az" || lang == "lt");
这一行看出,转小写的方法只对小部分的特殊语种做了处理
总结
没有特殊语种的情况直接使用无参toLowerCase即可,性能方面我没有做研究,有兴趣的可以去比较一下
边栏推荐
- 直播电商平台开发,点击查看更多显示所有内容
- workbench 数据导出
- Cysteine/Galactose/Perylenediimide Functionalized Fe3O4 Fe3O4 Nanomaterials | Scientific Research Reagents
- 一文搞懂│XSS攻击、SQL注入、CSRF攻击、DDOS攻击、DNS劫持
- 一道很简答但是没答对的SQL题
- VS2019常用快捷键
- 普罗米修斯原理及节点发布
- 22 high mid term paper topics forecast
- [R language] Extract all files under a folder to a specific folder
- Gao Zelong, a famous digital collection expert and founder of the Digital Collection Conference, was interviewed by China Entrepreneur Magazine
猜你喜欢
[email protected]@BSABiS纳米颗粒)|树状大分子稳定的硫化铋纳米颗粒|科研试剂"/>四氧化三铁/硫化铋纳米复合材料([email protected]@BSABiS纳米颗粒)|树状大分子稳定的硫化铋纳米颗粒|科研试剂

Xilinx Zynq ZynqMP DNA

中英文说明书丨TRC 交替醇(Catalogue NumberA575760)

ZIP压缩包文件删除密码的方法

list 字符串的输出方法 print(*a) print(““.join(str(c) for c in a) )

磁性核壳四氧化三铁颗粒负载金纳米星|磁性Fe3O4-POSS-COOH|超顺磁四氧化三铁聚多巴胺核壳结构纳米粒子

Unity backgammon game design and simple AI implementation (1)

聚酰胺-胺(PAMAM)树形聚合物-硫化铋复合纳米粒子|硫化铋修饰Gd‑DTPA‑OA配体|科研实验用

SiO2/KH550修饰四氧化三铁纳米磁性颗粒|PDA包裹四氧化三铁磁性纳米颗粒(科研级)

【Feel】In the Unity Feel plugin, Camera cannot display CameraShake correctly
随机推荐
Text String Length Sorting - Online Tool
【R语言】对文件进行归一化整理到各文件类型文件夹
Fe3O4/SiO2 Composite Magnetic Nanoparticles Aminated on SiO2-NH2/Fe3O4 Surface (Qiyue Reagent)
ZIP压缩包文件删除密码的方法
Adds, deletes, searches, and changes the leading doubly circular linked list (implemented in C language)
list 字符串的输出方法 print(*a) print(““.join(str(c) for c in a) )
A test engineer with an annual salary of 35W was laid off. Personal experience: advice that you have to listen to
年薪35W的测试工程师被裁亲身经验:不得不听的忠告
[GO], arrays and slices
qt发送邮件程序
Word文件的只读模式没有密码怎么退出?
Introduction of convenient functions and convenient shortcut keys of vs tomato assistant
Magnetic Core-Shell Fe3O4 Particles Supported Gold Nanostars | Magnetic Fe3O4-POSS-COOH | Superparamagnetic Fe3O4-Polydopamine Core-Shell Nanoparticles
电学知识的疑问
kubernetes 安全
TCP segment of a reassembled PDU
SiO2/KH550修饰四氧化三铁纳米磁性颗粒|PDA包裹四氧化三铁磁性纳米颗粒(科研级)
Unity C# 委托——事件,Action,Func的作用和区别
SiO2-NH2/Fe3O4表面氨基化的Fe3O4/SiO2复合磁性纳米粒子(齐岳试剂)
The 24th day of the special assault version of the sword offer