Guava CaseFormat类

来源:这里教程网 时间:2026-02-17 21:39:16 作者:

Guava CaseFormat类

CaseFormat是一个实用类,用于在各种ASCII字符格式之间进行转换。

类声明

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

@GwtCompatiblepublic enum CaseFormat   extends Enum<CaseFormat>

枚举常量

序号枚举常量及其描述
1LOWER_CAMEL Java变量命名约定,例如 “lowerCamel”。
2LOWER_HYPHEN 以连字符为命名约定的变量,例如 “lower-hyphen”。
3LOWER_UNDERSCORE C++变量命名约定,例如 “lower_underscore”。
4UPPER_CAMEL Java和C++类命名约定,例如 “UpperCamel”。
5UPPER_UNDERSCORE Java和C++常量命名约定,例如 “UPPER_UNDERSCORE”。

方法

序号方法与描述
1Converter <String, String> converterTo(CaseFormat targetFormat) 返回一个将字符串从该格式转换为目标格式的转换器。
2String to(CaseFormat format, String str) 将指定的字符串从该格式转换为指定的格式。
3static CaseFormat valueOf(String name) 返回具有指定名称的枚举常量。
4static CaseFormat[] values() 返回包含此枚举类型常量的数组,按照声明顺序排列。

继承的方法

这个类继承了以下类的方法−

java.lang.Enumjava.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

看结果。

testDatatestDataTestData

相关推荐