欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java8中字符串處理庫strman-java的使用示例

 更新時間:2016年09月22日 10:14:04   投稿:daisy  
除了Java本身的字符串處理方式外,我們還可以使用Apache Common Langs里的StringUtils來簡化String的操作。但以上兩種方式對于我們?nèi)粘>幊讨凶钊菀着龅降淖址幚韥碚f,仍然顯得有些不足。所以這篇文章給大家介紹Java8中字符串處理庫strman-java的使用。

介紹

Strmen-java是一個字符串處理工具,你可以通過maven將它引入到項目中。Strmen-java為我們提供了一個非常完整且強大的解決方案,使用它可以解決幾乎所有字符串處理場景。

使用

為了能在你的Java應(yīng)用程序中使用strman-java,可以把這個包下載下來添加到你項目的lib目錄中,如果使用的是Maven做項目管理,則只需要在你的pom.xml中加入如下依賴即可:

<dependency> 
 <groupId>com.shekhargulati</groupId>
 <artifactId>strman</artifactId>
 <version>0.2.0</version>
 <type>jar</type>
</dependency> 

如果是Gradle用戶則在build.gradle文件中添加如下代碼:

compile(group: 'com.shekhargulati', name: 'strman', version: '0.2.0', ext: 'jar'){ 
 transitive=true
}

示例

下面便是Strman-java的使用示例:

import strman.Strman; 
import java.util.Arrays; 
import java.util.Optional;

/**
 * strman-java包的測試使用類
 * Created by blinkfox on 16/7/17.
 */
public class StrmanTest {

 public static void main(String[] args) {
  // append 在一個字符串后追加任意個數(shù)的字符串
  String s1 = Strman.append("f", "o", "o", "b", "a", "r");
  System.out.println("append:" + s1); // result => "foobar"

  // prepend 在一個字符串前追加任意個數(shù)的字符串
  String s1pre = Strman.prepend("r", "f", "o", "o", "b", "a");
  System.out.println("prepend:" + s1pre); // result => "foobar"

  // appendArray 在一個字符串后先后追加一個字符串?dāng)?shù)組中的元素
  String s2 = Strman.appendArray("f", new String[]{"o", "o", "b", "a", "r"});
  System.out.println("append:" + s2); // result => "foobar"

  // at 根據(jù)字符串的索引獲取到對應(yīng)的字符。如果索引是負數(shù),則逆向獲取,超出則拋出異常
  Optional<String> s3 = Strman.at("foobar", 3);
  System.out.println("at:" + s3.get()); // result => "b"

  // between 得到一個字符串中,開始字符串和結(jié)束字符串之間的字符串的數(shù)組
  String[] s4 = Strman.between("[abc], [def]", "[", "]");
  System.out.println("between:" + Arrays.toString(s4)); // result => "[abc, def]"

  // chars 得到一個字符串中所有字符構(gòu)成的字符串?dāng)?shù)組
  String[] s5 = Strman.chars("title");
  System.out.println("chars:" + Arrays.toString(s5)); // result => "[t, i, t, l, e]"

  // collapseWhitespace 替換掉連續(xù)的多個空格為一個空格
  String s6 = Strman.collapseWhitespace("foo bar");
  System.out.println("chars:" + s6); // result => "foo bar"

  // contains 判斷一個字符串是否包含另外一個字符串,第三個參數(shù),表示字符串大小寫是否敏感
  boolean s7 = Strman.contains("foo bar", "foo");
  boolean s8 = Strman.contains("foo bar", "FOO", false);
  System.out.println("contains:" + s7 + ", " + s8); // result => "true, true"

  // containsAll 判斷一個字符串是否包含某字符串?dāng)?shù)組中的所有元素
  boolean s9 = Strman.containsAll("foo bar", new String[]{"foo", "bar"});
  boolean s10 = Strman.containsAll("foo bar", new String[]{"FOO", "bar"}, false);
  System.out.println("containsAll:" + s9 + ", " + s10); // result => "true, true"

  // containsAny 判斷一個字符串是否包含某字符串?dāng)?shù)組中的任意一個元素
  boolean s11 = Strman.containsAny("foo bar", new String[]{"FOO", "BAR", "Test"}, false);
  System.out.println("containsAny:" + s11); // result => "true"

  // countSubstr 判斷一個字符串包含某字符串的個數(shù)
  long s12 = Strman.countSubstr("aaaAAAaaa", "aaa");
  long s13 = Strman.countSubstr("aaaAAAaaa", "aaa", false, false);
  System.out.println("countSubstr:" + s12 + ", " + s13); // result => "2, 3"

  // endsWith 判斷一個字符串是否以某個字符串結(jié)尾
  boolean s14 = Strman.endsWith("foo bar", "bar");
  boolean s15 = Strman.endsWith("foo bar", "BAR", false);
  System.out.println("endsWith:" + s14 + ", " + s15); // result => "true, true"

  // ensureLeft 確保一個字符串以某個字符串開頭,如果不是,則在前面追加該字符串,并將字符串結(jié)果返回
  String s16 = Strman.ensureLeft("foobar", "foo");
  String s17 = Strman.ensureLeft("bar", "foo");
  String s18 = Strman.ensureLeft("foobar", "FOO", false);
  System.out.println("ensureLeft:" + s16 + ", " + s17 + ", " + s18);
  // result => "foobar, foobar, foobar"

  // ensureRight 確保一個字符串以某個字符串開頭,如果不是,則在前面追加該字符串,并將字符串結(jié)果返回
  String s16r = Strman.ensureRight("foobar", "bar");
  String s17r = Strman.ensureRight("foo", "bar");
  String s18r = Strman.ensureRight("fooBAR", "bar", false);
  System.out.println("ensureRight:" + s16r + ", " + s17r + ", " + s18r);
  // result => "foobar, foobar, fooBAR"

  // base64Encode 將字符串轉(zhuǎn)成Base64編碼的字符串
  String s19 = Strman.base64Encode("strman");
  System.out.println("base64Encode:" + s19); // result => "c3RybWFu"

  // binDecode 將二進制編碼(16位)轉(zhuǎn)成字符串字符
  String s20 = Strman.binDecode("0000000001000001");
  System.out.println("binDecode:" + s20); // result => "A"

  // binEncode 將字符串字符轉(zhuǎn)成二進制編碼(16位)
  String s21 = Strman.binEncode("A");
  System.out.println("binEncode:" + s21); // result => "0000000001000001"

  // decDecode 將十進制編碼(5位)轉(zhuǎn)成字符串字符
  String s22 = Strman.decDecode("00065");
  System.out.println("decDecode:" + s22); // result => "A"

  // decEncode 將字符串轉(zhuǎn)成十進制編碼(5位)
  String s23 = Strman.decEncode("A");
  System.out.println("decEncode:" + s23); // result => "00065"

  // first 得到從字符串開始到索引n的字符串
  String s24 = Strman.first("foobar", 3);
  System.out.println("first:" + s24); // result => "foo"

  // last 得到從字符串結(jié)尾倒數(shù)索引n的字符串
  String s24l = Strman.last("foobar", 3);
  System.out.println("last:" + s24l); // result => "bar"

  // head 得到字符串的第一個字符
  String s25 = Strman.head("foobar");
  System.out.println("head:" + s25); // result => "f"

  // hexDecode 將字符串字符轉(zhuǎn)成十六進制編碼(4位)
  String s26 = Strman.hexDecode("0041");
  System.out.println("hexDecode:" + s26); // result => "A"

  // hexEncode 將十六進制編碼(4位)轉(zhuǎn)成字符串字符
  String s27 = Strman.hexEncode("A");
  System.out.println("hexEncode:" + s27); // result => "0041"

  // inequal 測試兩個字符串是否相等
  boolean s28 = Strman.inequal("a", "b");
  System.out.println("inequal:" + s28); // result => "true"

  // insert 將子字符串插入到字符串某索引位置處
  String s29 = Strman.insert("fbar", "oo", 1);
  System.out.println("insert:" + s29); // result => "foobar"

  // leftPad 將字符串從左補齊直到總長度為n為止
  String s30 = Strman.leftPad("1", "0", 5);
  System.out.println("leftPad:" + s30); // result => "00001"

  // rightPad 將字符串從右補齊直到總長度為n為止
  String s30r = Strman.rightPad("1", "0", 5);
  System.out.println("rightPad:" + s30r); // result => "10000"

  // lastIndexOf 此方法返回在指定值的最后一個發(fā)生的調(diào)用字符串對象中的索引,從偏移量中向后搜索
  int s31 = Strman.lastIndexOf("foobarfoobar", "F", false);
  System.out.println("lastIndexOf:" + s31); // result => "6"

  // leftTrim 移除字符串最左邊的所有空格
  String s32 = Strman.leftTrim(" strman ");
  System.out.println("leftTrim:" + s32); // result => "strman "

  // rightTrim 移除字符串最右邊的所有空格
  String s32r = Strman.rightTrim(" strman ");
  System.out.println("rightTrim:" + s32r); // result => " strman"

  // removeEmptyStrings 移除字符串?dāng)?shù)組中的空字符串
  String[] s33 = Strman.removeEmptyStrings(new String[]{"aa", "", " ", "bb", "cc", null});
  System.out.println("removeEmptyStrings:" + Arrays.toString(s33));
  // result => "[aa, bb, cc]"

  // removeLeft 得到去掉前綴(如果存在的話)后的新字符串
  String s34 = Strman.removeLeft("foobar", "foo");
  System.out.println("removeLeft:" + s34); // result => "bar"

  // removeRight 得到去掉后綴(如果存在的話)后的新字符串
  String s34r = Strman.removeRight("foobar", "bar");
  System.out.println("removeRight:" + s34r); // result => "foo"

  // removeNonWords 得到去掉不是字符的字符串
  String s35 = Strman.removeNonWords("foo&bar-");
  System.out.println("removeNonWords:" + s35); // result => "foobar"

  // removeSpaces 移除所有空格
  String s36 = Strman.removeSpaces(" str man ");
  System.out.println("removeSpaces:" + s36); // result => " strman"

  // repeat 得到給定字符串和重復(fù)次數(shù)的新字符串
  String s37 = Strman.repeat("1", 3);
  System.out.println("repeat:" + s37); // result => "111"

  // reverse 得到反轉(zhuǎn)后的字符串
  String s38 = Strman.reverse("foobar");
  System.out.println("reverse:" + s38); // result => "raboof"

  // safeTruncate 安全的截斷字符串,不切一個字的一半,它總是返回最后一個完整的單詞
  String s39 = Strman.safeTruncate("A Javascript string manipulation library.", 19, "...");
  System.out.println("safeTruncate:" + s39); // result => "A Javascript..."

  // truncate 不太安全的截斷字符串
  String s40 = Strman.truncate("A Javascript string manipulation library.", 19, "...");
  System.out.println("truncate:" + s40); // result => "A Javascript str..."

  // htmlDecode 將html字符反轉(zhuǎn)義
  String s41 = Strman.htmlDecode("&SHcy;");
  System.out.println("htmlDecode:" + s41); // result => "Ш"

  // htmlEncode 將html字符轉(zhuǎn)義
  String s42 = Strman.htmlEncode("Ш");
  System.out.println("htmlEncode:" + s42); // result => "&SHcy;"

  // shuffle 將給定字符串轉(zhuǎn)成隨機字符順序的字符串
  String s43 = Strman.shuffle("shekhar");
  System.out.println("shuffle:" + s43); // result => "rhsheak"

  // slugify 將字符串分段(用"-"分段)
  String s44 = Strman.slugify("foo bar");
  System.out.println("slugify:" + s44); // result => "foo-bar"

  // transliterate 刪除所有非有效字符,如:á => a
  String s45 = Strman.transliterate("fóõ bár");
  System.out.println("transliterate:" + s45); // result => "foo bar"

  // surround 給定的“前綴”和“后綴”來包裹一個字符串
  String s46 = Strman.surround("div", "<", ">");
  System.out.println("surround:" + s46); // result => "<div>"

  // tail 得到去掉第一個字符后的字符串
  String s47 = Strman.tail("foobar");
  System.out.println("tail:" + s47); // result => "oobar"

  // toCamelCase 轉(zhuǎn)成駝峰式的字符串
  String s48 = Strman.toCamelCase("Camel Case");
  String s48_2 = Strman.toCamelCase("camel-case");
  System.out.println("tail:" + s48 + ", " + s48_2); // result => "camelCase, camelCase"

  // toStudlyCase 轉(zhuǎn)成Studly式的字符串
  String s49 = Strman.toStudlyCase("hello world");
  System.out.println("toStudlyCase:" + s49); // result => "HelloWorld"

  // toDecamelize 轉(zhuǎn)成Decamelize式的字符串
  String s50 = Strman.toDecamelize("helloWorld", null);
  System.out.println("toDecamelize:" + s50); // result => "hello world"

  // toKebabCase 轉(zhuǎn)成Kebab式的字符串
  String s51 = Strman.toKebabCase("hello World");
  System.out.println("toKebabCase:" + s51); // result => "hello-world"

  // toSnakeCase 轉(zhuǎn)成Snake式的字符串
  String s52 = Strman.toSnakeCase("hello world");
  System.out.println("toSnakeCase:" + s52); // result => "hello_world"
 }

}

總結(jié)

以上就是這篇文章的全部內(nèi)容,希望能對大家的學(xué)習(xí)或者工作帶來一定的幫助,如果有疑問大家可以留言交流。

相關(guān)文章

  • 詳解Java編程中JavaMail API的使用

    詳解Java編程中JavaMail API的使用

    這篇文章主要介紹了詳解Java編程中JavaMail API的使用,通過JavaMail可以實現(xiàn)豐富的郵件類相關(guān)功能,需要的朋友可以參考下
    2015-11-11
  • java拓展集合工具類CollectionUtils

    java拓展集合工具類CollectionUtils

    這篇文章主要為大家詳細介紹了java拓展集合工具類CollectionUtils,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-03-03
  • SpringBoot自定義Starter及使用

    SpringBoot自定義Starter及使用

    這篇文章主要介紹了SpringBoot自定義Starter及使用,Starter是Spring Boot中的一個非常重要的概念,Starter相當(dāng)于模塊,它能將模塊所需的依賴整合起來并對模塊內(nèi)的Bean根據(jù)環(huán)境進行自動配置,需要的朋友可以參考下
    2023-07-07
  • 詳解SpringBoot之訪問靜態(tài)資源(webapp...)

    詳解SpringBoot之訪問靜態(tài)資源(webapp...)

    這篇文章主要介紹了詳解SpringBoot之訪問靜態(tài)資源(webapp...),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • Java設(shè)計模式之工廠模式案例詳解

    Java設(shè)計模式之工廠模式案例詳解

    工廠模式(Factory Pattern)是Java中最常用的設(shè)計模式之一。這種類型的設(shè)計模式屬于創(chuàng)建型模式,它提供了一種創(chuàng)建對象的最佳方式。本文將通過案例詳細講解一下工廠模式,需要的可以參考一下
    2022-02-02
  • java開發(fā)環(huán)境的完整搭建過程

    java開發(fā)環(huán)境的完整搭建過程

    這篇文章主要給大家介紹了關(guān)于java開發(fā)環(huán)境的完整搭建過程,文中介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-02-02
  • SpringMVC?Restful風(fēng)格與中文亂碼問題解決方案介紹

    SpringMVC?Restful風(fēng)格與中文亂碼問題解決方案介紹

    Restful就是一個資源定位及資源操作的風(fēng)格,不是標(biāo)準也不是協(xié)議,只是一種風(fēng)格,是對http協(xié)議的詮釋,下面這篇文章主要給大家介紹了關(guān)于SpringMVC對Restful風(fēng)格支持的相關(guān)資料,需要的朋友可以參考下
    2022-10-10
  • JAVA中取整數(shù)的4種方法總結(jié)

    JAVA中取整數(shù)的4種方法總結(jié)

    這篇文章主要給大家介紹了關(guān)于JAVA中取整數(shù)的4種方法,在java的Math類中,提供了許許多多的和數(shù)學(xué)計算有關(guān)的方法,其中也包括取整的,需要的朋友可以參考下
    2023-07-07
  • Java 添加、更新和移除PDF超鏈接的實現(xiàn)方法

    Java 添加、更新和移除PDF超鏈接的實現(xiàn)方法

    PDF超鏈接用一個簡單的鏈接包含了大量的信息,滿足了人們在不占用太多空間的情況下渲染外部信息的需求。這篇文章主要介紹了Java 添加、更新和移除PDF超鏈接的實現(xiàn)方法,需要的朋友可以參考下
    2019-05-05
  • maven引入本地jar包運行報錯java.lang.NoClassDefFoundError解決

    maven引入本地jar包運行報錯java.lang.NoClassDefFoundError解決

    這篇文章主要為大家介紹了maven引入本地jar包運行報錯java.lang.NoClassDefFoundError解決方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-10-10

最新評論