- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
情况:有一个 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/
我想知道为什么 Character.toUpperCase/toLowerCase 没有像 String.toUpperCase/toLowerCase 这样的 Locale 参数。 我必须将任何语言
这是我的程序的代码。主要方法已提供,不可触及。 main 方法将 String 转换为大写,但不接受 toUpperCase(); java方法。如何在不改变 main 方法的情况下解决这个问题? p
我们需要从大写字符串中获取字符流。有两种方法: word.toUpperCase().chars() word.chars().map(Character::toUpperCase) 哪种方法更好?
我想知道为什么 Character.toUpperCase/toLowerCase 没有像 String.toUpperCase/toLowerCase 这样的 Locale 参数。 我必须首先将可以
我正在尝试以这种特定的方式解决编码挑战。 目标是将字符串的每个第二个字母大写。 当我使用 letterArray[0] 或任何其他数字运行代码时,代码运行完美。但当我将其切换到 letterArray
我有以下字符串数组 let example = ["hello", "what"]; 我想将此数组转换为: ["Hello", "what"]; 为了实现这一点,我有以下代码: example[0]
当名称全部为大写时,该函数应该向用户喊回。例如,当名称为“JERRY”时,函数应返回字符串“HELLO, JERRY!”控制台记录错误:.toUpperCase() 不是函数。 var hello =
这个问题已经有答案了: toUpperCase() is not making the string upper case (9 个回答) 已关闭 2 年前。 我是 Javascript 新手,对 t
就在前几天,我遇到了一个奇怪的错误。我必须构建一串字符。作为分隔符,我正在与之通信的主机系统使用了 char 254。无论如何,我构建了我的字符串并将其发送到主机。在主机上我收到 char 222 作
这个问题已经有答案了: toUpperCase() is not making the string upper case (9 个回答) 已关闭 2 年前。 我是 Javascript 新手,对 t
这个问题已经有答案了: 已关闭10 年前。 Possible Duplicate: How do I compare strings in Java? Strings in Java : equals
典型输入如下... Department one 在我的函数中... var chksDept = $('input[type=checkbox].actEmail:checked'); var de
我想在每次更改输入字段的内容时执行 toUpperCase()。我的意思不是当字段失去焦点时,而是每次键入字符时。 我尝试了这个,但没有用(我对 JQ 还很陌生)! $(".myClass").cha
if (json.ScheduleCareProviderScheduledPatientsGetResult.Appointments[i].Patient.Firstname.toUpperCas
我不知道我做错了什么;但不知何故 .toUpperCase() 字符串函数在我的浏览器上不起作用还是我弄错了什么? var string ="kjsdgfiIJHBVSFIU"; string.toU
我正在尝试解决一个问题,该问题采用特定符号并将其从字符串中删除,然后将该符号后面的字符转换为大写。但由于某种原因,大写字符后面的字符也变成大写。 s 是给定的字符串。 String[] symbols
我正在尝试编写一个接受字符串的函数,该字符串将以数组内的“waved”版本返回: wave("stack") => ["Stack","sTack", "stAck", "staCk", "stack
这就是我想要的代码。当您写入任何单词时,它会使用 onkeyup 自动更改为大写。此外,当用户单击新窗口时,大写字母将显示在新窗口中。我只能做第一部分。我该怎么做呢? https://jsfiddle
我正在尝试修复由forbiddenapis 报告的错误。我有这样一句话: paramMap.put(Config.TITLEBOOST.toUpperCase(), titleBoost); 所以,它
再说一遍:我在这里学习!很抱歉回答这些基本问题,但我必须学习...... 使用我在网上读到的一个技巧,我试图将其从全部大写改为大写首字母。请参阅此处的代码 - 特别是我们正在查看 JavaScript
我是一名优秀的程序员,十分优秀!