欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java8中字符串处理库strman-java的使用示例

程序员文章站 2024-03-12 18:43:56
介绍 strmen-java是一个字符串处理工具,你可以通过maven将它引入到项目中。strmen-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("&shcy;");
  system.out.println("htmldecode:" + s41); // result => "ш"

  // htmlencode 将html字符转义
  string s42 = strman.htmlencode("ш");
  system.out.println("htmlencode:" + s42); // result => "&shcy;"

  // 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"
 }

}

总结

以上就是这篇文章的全部内容,希望能对大家的学习或者工作带来一定的帮助,如果有疑问大家可以留言交流。