language-agnostic - 重新实现 ToUpper()

标签 language-agnostic unicode localization internationalization toupper

如果 ToUpper() 不存在,你会怎么写? i18n 和 L10n 的奖励积分

由此引发的好奇心:http://thedailywtf.com/Articles/The-Long-Way-toUpper.aspx

最佳答案

  • 我下载了 Unicode 表
  • 我将表导入数据库
  • 我写了一个方法 upper()。

  • 这是一个示例实现;)
    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”。如果你找到它,你就找到了大写版本。

    关于language-agnostic - 重新实现 ToUpper(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/333946/

    相关文章:

    c# - Winforms 本地化错误

    ios - 如何本地化应用程序图标?

    algorithm - 当键占用超过一个单词的内存时,合并排序/基数排序的复杂性是否会改变

    language-agnostic - 向量、集合和元组之间的差异

    windows - 如何使用 Perl 在 Windows 中创建 unicode 文件名

    java - 如何在jetty-util-ajax中使用JSON.toString()方法转义Unicode?

    language-agnostic - 有符号变量和无符号变量有什么区别?

    xml - 在单元测试中验证 XML 的最佳方法是什么?

    java - 如何在 TextView 中使用方向箭头

    java - Liferay 6.2 中的翻译进度