gpt4 book ai didi

java - Java String.toUpperCase() 会失败吗?

转载 作者:行者123 更新时间:2023-11-29 04:50:26 24 4
gpt4 key购买 nike

情况:有一个 Java ESB,它正在从 Vaadin 网络表单获取输入(姓氏),并且应该保证在将其保存到数据库之前将其大写。

我被指派调查一个报告的问题,即小写字符有时会出现在数据库中。我了解到,该程序在通过 EntityManager 保存数据之前正在使用 String.toUpperCase()(这是唯一修改接收到的数据的地方)。

所以我想知道的是,这是否足够。到目前为止,我还没有发现任何与 toUpperCase() 函数相关的“众所周知”的问题,但我想确定一下。

那么问题来了——String.toUpperCase() 是否总能完成它的工作?还是有什么可能的字符或情况会出错导致字母不能大写?

最佳答案

Can Java String.toUpperCase() ever fail?

这取决于您是否传递区域设置敏感字符串(见下文)。


Java.lang.String 的实现中,它只是使用默认的语言环境:

public String toUpperCase() {
return toUpperCase(Locale.getDefault());
}

toUpperCase(Locale) 使用给定 Locale 的规则将此 String 中的所有字符转换为大写。大小写映射基于 Character 类指定的 Unicode 标准版本。由于大小写映射并不总是 1:1 字符映射,因此生成的字符串可能与原始字符串不同的长度

This method is locale sensitive, and may produce unexpected results if used for strings that are intended to be interpreted locale independently. Examples are programming language identifiers, protocol keys, and HTML tags.

To obtain correct results for locale insensitive strings, use toUpperCase(Locale.ENGLISH).

如果您对 toUpperCase(Locale) 的实现方式感兴趣:

public String toUpperCase(Locale locale) {
if (locale == null) {
throw new NullPointerException();
}

int firstLower;
final int len = value.length;

/* Now check if there are any characters that need to be changed. */
scan: {
for (firstLower = 0 ; firstLower < len; ) {
int c = (int)value[firstLower];
int srcCount;
if ((c >= Character.MIN_HIGH_SURROGATE)
&& (c <= Character.MAX_HIGH_SURROGATE)) {
c = codePointAt(firstLower);
srcCount = Character.charCount(c);
} else {
srcCount = 1;
}
int upperCaseChar = Character.toUpperCaseEx(c);
if ((upperCaseChar == Character.ERROR)
|| (c != upperCaseChar)) {
break scan;
}
firstLower += srcCount;
}
return this;
}

/* result may grow, so i+resultOffset is the write location in result */
int resultOffset = 0;
char[] result = new char[len]; /* may grow */

/* Just copy the first few upperCase characters. */
System.arraycopy(value, 0, result, 0, firstLower);

String lang = locale.getLanguage();
boolean localeDependent =
(lang == "tr" || lang == "az" || lang == "lt");
char[] upperCharArray;
int upperChar;
int srcChar;
int srcCount;
for (int i = firstLower; 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) {
upperChar = ConditionalSpecialCasing.toUpperCaseEx(this, i, locale);
} else {
upperChar = Character.toUpperCaseEx(srcChar);
}
if ((upperChar == Character.ERROR)
|| (upperChar >= Character.MIN_SUPPLEMENTARY_CODE_POINT)) {
if (upperChar == Character.ERROR) {
if (localeDependent) {
upperCharArray =
ConditionalSpecialCasing.toUpperCaseCharArray(this, i, locale);
} else {
upperCharArray = Character.toUpperCaseCharArray(srcChar);
}
} else if (srcCount == 2) {
resultOffset += Character.toChars(upperChar, result, i + resultOffset) - srcCount;
continue;
} else {
upperCharArray = Character.toChars(upperChar);
}

/* Grow result if needed */
int mapLen = upperCharArray.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] = upperCharArray[x];
}
resultOffset += (mapLen - srcCount);
} else {
result[i + resultOffset] = (char)upperChar;
}
}
return new String(result, 0, len + resultOffset);
}

关于java - Java String.toUpperCase() 会失败吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35622834/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com