zoukankan      html  css  js  c++  java
  • Guava之CaseFormat

      com.google.common.base.CaseFormat是一种实用工具类,以提供不同的ASCII字符格式之间的转换。

    其对应的枚举常量

        

      从以上枚举中可以看出,java程序员最常用的转换类型为:UPPER_CAMEL,即我们常说的"驼峰式"编写方式;其次,我们常用的是:UPPER_UNDERSCORE,即我们常用的常量命名法,不同单词见使用下划线分割的书写方式。

    对应有的方法

       

    CaseFormat 示例

    public static void main(String args[]) {
            CaseFormatTest tester = new CaseFormatTest();
            tester.testCaseFormat();
        }
    
        private void testCaseFormat() {
            System.out.println(CaseFormat.LOWER_HYPHEN.to(CaseFormat.LOWER_CAMEL, "test-data"));
            System.out.println(CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, "test_data"));
            System.out.println(CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, "test_data"));
            
            System.out.println(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, "testdata"));
            System.out.println(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, "TestData"));
            System.out.println(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_HYPHEN, "testData"));
        }

    运行结果如下:

    testData
    testData
    TestData

    testdata
    test_data
    test-data

       从以上结果我们可以分析,倒数第3个结果没有转换成功,将原字符串打印出来,推导出guava不可能做到那么智能,能够自动识别一个单词之后将两个单词用下划线分隔开(要想做也是可以的,需要将英文词库加载一遍,之后每次扫描给定的字符串进行单词分割,这样做就太复杂了),它只能通过给定字符串大小写的方式或者上面3个实例有分隔符的方式才能拆分开,故倒数第二个就能正确识别出来。所以在使用guava转换的时候一定要注意了。

  • 相关阅读:
    R tips
    向量化与并行计算
    cf relevent R package
    一篇关于相似性解释的文章,写得非常的仔细
    lsa cosine R
    install lsa package for R on ubuntu 10.04 lts lucid
    updatealternatives error no alternatives for xulrunner1.9javaplugin.so 问题解决
    《如何在windows程序中读取bios内容》
    php数组根据某一个键值,把相同键值的合并生成一个新的二维数组
    阿里云域名备案之如何填写真实性核验单
  • 原文地址:https://www.cnblogs.com/liang1101/p/6435920.html
Copyright © 2011-2022 走看看