首頁 > Java > java教程 > 主體

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

PHPz
發布: 2023-09-13 09:33:03
轉載
826 人瀏覽過

使用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
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!