Guava - CaseFormat 类



CaseFormat 是一个工具类,用于提供各种ASCII字符格式之间的转换。

类声明

以下是com.google.common.base.CaseFormat类的声明:

@GwtCompatible
public enum CaseFormat
   extends Enum<CaseFormat>

枚举常量

序号 枚举常量及描述
1

LOWER_CAMEL

Java变量命名约定,例如:“lowerCamel”。

2

LOWER_HYPHEN

带连字符的变量命名约定,例如:“lower-hyphen”。

3

LOWER_UNDERSCORE

C++变量命名约定,例如:“lower_underscore”。

4

UPPER_CAMEL

Java和C++类命名约定,例如:“UpperCamel”。

5

UPPER_UNDERSCORE

Java和C++常量命名约定,例如:“UPPER_UNDERSCORE”。

方法

序号 方法及描述
1

Converter<String,String> converterTo(CaseFormat targetFormat)

返回一个转换器,用于将字符串从此格式转换为targetFormat。

2

String to(CaseFormat format, String str)

将指定的字符串str从此格式转换为指定的格式。

3

static CaseFormat valueOf(String name)

返回具有指定名称的此类型的枚举常量。

4

static CaseFormat[] values()

返回一个包含此枚举类型的常量数组,按声明顺序排列。

继承的方法

此类继承自以下类的方法:

  • java.lang.Enum
  • java.lang.Object

CaseFormat 类示例

使用您选择的任何编辑器创建以下Java程序,例如在C:/> Guava目录下。

GuavaTester.java

import com.google.common.base.CaseFormat;

public class GuavaTester {
   public static void main(String args[]) {
      GuavaTester tester = new GuavaTester();
      tester.testCaseFormat();
   }

   private void testCaseFormat() {
      String data = "test_data";
      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"));
   }
}

验证结果

使用javac编译器编译类,如下所示:

C:\Guava>javac GuavaTester.java

现在运行GuavaTester查看结果。

C:\Guava>java GuavaTester

查看结果。

testData
testData
TestData
guava_string_utilities.htm
广告
© . All rights reserved.