Java 中的 CaseFormat 類


CaseFormat 類是用於在各種 ASCII 案例格式之間進行轉換的實用類 −

修飾符和型別方法和描述
物件clone()
覆蓋可克隆。
布林值equals(Object obj)
覆蓋 equals.
String。format(double number)
格式化專業化。
抽象 StringBufferformat(double number, StringBuffer toAppendTo, FieldPosition pos)
格式化專業化。
Stringformat(long number)
格式化專業化。
抽象 StringBufferformat(long number, StringBuffer toAppendTo, FieldPosition pos)
格式化專業化。

示例

現在我們來看一個使用 Java 檔案 GuavaTester.java 實現 CaseFormat 類的示例 −

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

更新於: 02-Jan-2020

60 次瀏覽

開啟您的職業

完成課程以獲得認證

開始學習
廣告
© . All rights reserved.