如果ToUpper()不存在,你会如何编写它?如果能支持i18n和L10n就更好了。
这个问题的灵感来自于这篇文章:http://thedailywtf.com/Articles/The-Long-Way-toUpper.aspx。
这个问题的灵感来自于这篇文章:http://thedailywtf.com/Articles/The-Long-Way-toUpper.aspx。
这是一个示例实现 ;)
public static String upper(String s) {
if (s == null) {
return null;
}
final int N = s.length(); // Mind the optimization!
PreparedStatement stmtName = null;
PreparedStatement stmtSmall = null;
ResultSet rsName = null;
ResultSet rsSmall = null;
StringBuilder buffer = new StringBuilder (N); // Much faster than StringBuffer!
try {
conn = DBFactory.getConnection();
stmtName = conn.prepareStatement("select name from unicode.chart where codepoint = ?");
// TODO Optimization: Maybe move this in the if() so we don't create this
// unless there are uppercase characters in the string.
stmtSmall = conn.prepareStatement("select codepoint from unicode.chart where name = ?");
for (int i=0; i<N; i++) {
int c = s.charAt(i);
stmtName.setInt(1, c);
rsName = stmtName.execute();
if (rsName.next()) {
String name = rsName.getString(1);
if (name.contains(" SMALL ")) {
name = name.replaceAll(" SMALL ", " CAPITAL ");
stmtSmall.setString(1, name);
rsSmall = stmtSmall.execute();
if (rsSmall.next()) {
c = rsSmall.getInt(1);
}
rsSmall = DBUtil.close(rsSmall);
}
}
rsName = DBUtil.close(rsName);
}
}
finally {
// Always clean up
rsSmall = DBUtil.close(rsSmall);
rsName = DBUtil.close(rsName);
stmtSmall = DBUtil.close(stmtSmall);
stmtName = DBUtil.close(stmtName);
}
// TODO Optimization: Maybe read the table once into RAM at the start
// Would waste a lot of memory, though :/
return buffer.toString();
}
注意:您可以在unicode.org上找到的Unicode图表中包含字符/代码点的名称。这个字符串将包含大写字母的字符,其名称中包含“SMALL”(请注意空格,否则可能会匹配“SMALLER”等)。现在,您可以搜索一个类似名称,并将“SMALL”替换为“CAPITAL”。如果找到了它,您就找到了大写版。
我不认为SO能够在一篇帖子中处理Unicode表的大小 :)
不幸的是,仅仅使用char.ToUpper()来转换每个字符并不那么简单。
例如:
(string-upcase "Straße") ⇒ "STRASSE"
(string-downcase "Straße") ⇒ "straße"
(string-upcase "ΧΑΟΣ") ⇒ "ΧΑΟΣ"
(string-downcase "ΧΑΟΣ") ⇒ "χαος"
(string-downcase "ΧΑΟΣΣ") ⇒ "χαοσς"
(string-downcase "ΧΑΟΣ Σ") ⇒ "χαος σ"
(string-upcase "χαος") ⇒ "ΧΑΟΣ"
(string-upcase "χαοσ") ⇒ "ΧΑΟΣ"
没有静态表格是足够的,因为您需要在了解正确的转换之前知道语言。
例如,在土耳其语中,i
需要变成 İ
(U+0130),而在其他任何语言中,它需要变成 I
(U+0049)。而这个 i
是相同的字符 U+0069。
我不会赢得奖励分,但这是7位ASCII码:
char toupper(char c)
{
if ((c < 'a') || (c > 'z')) { return c; }
else { return c & 0xdf; }
}
让我建议为希伯来语、阿拉伯语、格鲁吉亚语等没有大写字母的语言提供更多的奖励分数。 :-)
在Python中...
touppe_map = { massive dictionary to handle all cases in all languages }
def to_upper( c ):
return toupper_map.get( c, c )
或者,如果你想用“错误的方式”来做
def to_upper( c ):
for k,v in toupper_map.items():
if k == c: return v
return c