程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> Java8中字符串處置庫strman-java的應用示例

Java8中字符串處置庫strman-java的應用示例

編輯:關於JAVA

Java8中字符串處置庫strman-java的應用示例。本站提示廣大學習愛好者:(Java8中字符串處置庫strman-java的應用示例)文章只能為提供參考,不一定能成為您想要的結果。以下是Java8中字符串處置庫strman-java的應用示例正文


引見

Strmen-java是一個字符串處置對象,你可以經由過程maven將它引入到項目中。Strmen-java為我們供給了一個異常完全且壯大的處理計劃,應用它可以處理簡直一切字符串處置場景。

應用

為了能在你的Java運用法式中應用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 在一個字符串後追加隨意率性個數的字符串
  String s1 = Strman.append("f", "o", "o", "b", "a", "r");
  System.out.println("append:" + s1); // result => "foobar"

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

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

  // at 依據字符串的索引獲得到對應的字符。假如索引是正數,則逆向獲得,超越則拋出異常
  Optional<String> s3 = Strman.at("foobar", 3);
  System.out.println("at:" + s3.get()); // result => "b"

  // between 獲得一個字符串中,開端字符串和停止字符串之間的字符串的數組
  String[] s4 = Strman.between("[abc], [def]", "[", "]");
  System.out.println("between:" + Arrays.toString(s4)); // result => "[abc, def]"

  // chars 獲得一個字符串中一切字符組成的字符串數組
  String[] s5 = Strman.chars("title");
  System.out.println("chars:" + Arrays.toString(s5)); // result => "[t, i, t, l, e]"

  // collapseWhitespace 調換失落持續的多個空格為一個空格
  String s6 = Strman.collapseWhitespace("foo bar");
  System.out.println("chars:" + s6); // result => "foo bar"

  // contains 斷定一個字符串能否包括別的一個字符串,第三個參數,表現字符串年夜小寫能否敏感
  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 斷定一個字符串能否包括某字符串數組中的一切元素
  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 斷定一個字符串能否包括某字符串數組中的隨意率性一個元素
  boolean s11 = Strman.containsAny("foo bar", new String[]{"FOO", "BAR", "Test"}, false);
  System.out.println("containsAny:" + s11); // result => "true"

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

  // endsWith 斷定一個字符串能否以某個字符串開頭
  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 確保一個字符串以某個字符串開首,假如不是,則在後面追加該字符串,並將字符串成果前往
  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 確保一個字符串以某個字符串開首,假如不是,則在後面追加該字符串,並將字符串成果前往
  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 將字符串轉成Base64編碼的字符串
  String s19 = Strman.base64Encode("strman");
  System.out.println("base64Encode:" + s19); // result => "c3RybWFu"

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

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

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

  // decEncode 將字符串轉成十進制編碼(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 獲得從字符串開頭倒數索引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 將字符串字符轉成十六進制編碼(4位)
  String s26 = Strman.hexDecode("0041");
  System.out.println("hexDecode:" + s26); // result => "A"

  // hexEncode 將十六進制編碼(4位)轉成字符串字符
  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 此辦法前往在指定值的最初一個產生的挪用字符串對象中的索引,從偏移量中向後搜刮
  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 移除字符串數組中的空字符串
  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 獲得給定字符串和反復次數的新字符串
  String s37 = Strman.repeat("1", 3);
  System.out.println("repeat:" + s37); // result => "111"

  // reverse 獲得反轉後的字符串
  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字符反本義
  String s41 = Strman.htmlDecode("Ш");
  System.out.println("htmlDecode:" + s41); // result => "Ш"

  // htmlEncode 將html字符本義
  String s42 = Strman.htmlEncode("Ш");
  System.out.println("htmlEncode:" + s42); // result => "Ш"

  // shuffle 將給定字符串轉成隨機字符次序的字符串
  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 轉成駝峰式的字符串
  String s48 = Strman.toCamelCase("Camel Case");
  String s48_2 = Strman.toCamelCase("camel-case");
  System.out.println("tail:" + s48 + ", " + s48_2); // result => "camelCase, camelCase"

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

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

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

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

}

總結

以上就是這篇文章的全體內容,願望能對年夜家的進修或許任務帶來必定的贊助,假如有疑問年夜家可以留言交換。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved