首頁  >  文章  >  Java  >  使用Java中的Apache Commons庫從字串中刪除前導零

使用Java中的Apache Commons庫從字串中刪除前導零

PHPz
PHPz轉載
2023-09-13 09:33:03803瀏覽

使用Java中的Apache Commons库从字符串中删除前导零

org.apache.commons.lang.StringUtils 類別的stripStart() 方法接受兩個字串,並從字串中刪除第二個字串表示的字元集。第一個字串的字串。

使用apache 公用庫從字串中刪除前導零-

  • #將以下相依性新增至您的pom .xml 檔案

<dependency>
   <groupId>org.apache.commons</groupId>
   <artifactId>commons-lang3</artifactId>
   <version>3.9</version>
</dependency>
  • 取得字串。

  • 將取得的字串作為第一個參數,將包含0 的字串作為第二個參數傳遞給StringUtils 類別的Strong>stripStart( ) 方法。

範例

以下Java 程式讀取將使用者的整數值轉換為字串,並使用StringUtils 類別的stripStart() 方法從中刪除前導零。

import java.util.Scanner;
import org.apache.commons.lang3.StringUtils;
public class LeadingZeroesCommons {
   public static void main(String args[]) {
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter a String: ");
      String str = sc.nextLine();
      String result = StringUtils.stripStart(str, "0");
      System.out.println(result);
   }
}

輸出

Enter a String:
000Hello how are you
Hello how are you

以上是使用Java中的Apache Commons庫從字串中刪除前導零的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除