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.