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的使用示例:
importstrman.Strman; importjava.util.Arrays; importjava.util.Optional; /** *strman-java包的测试使用类 *Createdbyblinkfoxon16/7/17. */ publicclassStrmanTest{ publicstaticvoidmain(String[]args){ //append在一个字符串后追加任意个数的字符串 Strings1=Strman.append("f","o","o","b","a","r"); System.out.println("append:"+s1);//result=>"foobar" //prepend在一个字符串前追加任意个数的字符串 Strings1pre=Strman.prepend("r","f","o","o","b","a"); System.out.println("prepend:"+s1pre);//result=>"foobar" //appendArray在一个字符串后先后追加一个字符串数组中的元素 Strings2=Strman.appendArray("f",newString[]{"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替换掉连续的多个空格为一个空格 Strings6=Strman.collapseWhitespace("foobar"); System.out.println("chars:"+s6);//result=>"foobar" //contains判断一个字符串是否包含另外一个字符串,第三个参数,表示字符串大小写是否敏感 booleans7=Strman.contains("foobar","foo"); booleans8=Strman.contains("foobar","FOO",false); System.out.println("contains:"+s7+","+s8);//result=>"true,true" //containsAll判断一个字符串是否包含某字符串数组中的所有元素 booleans9=Strman.containsAll("foobar",newString[]{"foo","bar"}); booleans10=Strman.containsAll("foobar",newString[]{"FOO","bar"},false); System.out.println("containsAll:"+s9+","+s10);//result=>"true,true" //containsAny判断一个字符串是否包含某字符串数组中的任意一个元素 booleans11=Strman.containsAny("foobar",newString[]{"FOO","BAR","Test"},false); System.out.println("containsAny:"+s11);//result=>"true" //countSubstr判断一个字符串包含某字符串的个数 longs12=Strman.countSubstr("aaaAAAaaa","aaa"); longs13=Strman.countSubstr("aaaAAAaaa","aaa",false,false); System.out.println("countSubstr:"+s12+","+s13);//result=>"2,3" //endsWith判断一个字符串是否以某个字符串结尾 booleans14=Strman.endsWith("foobar","bar"); booleans15=Strman.endsWith("foobar","BAR",false); System.out.println("endsWith:"+s14+","+s15);//result=>"true,true" //ensureLeft确保一个字符串以某个字符串开头,如果不是,则在前面追加该字符串,并将字符串结果返回 Strings16=Strman.ensureLeft("foobar","foo"); Strings17=Strman.ensureLeft("bar","foo"); Strings18=Strman.ensureLeft("foobar","FOO",false); System.out.println("ensureLeft:"+s16+","+s17+","+s18); //result=>"foobar,foobar,foobar" //ensureRight确保一个字符串以某个字符串开头,如果不是,则在前面追加该字符串,并将字符串结果返回 Strings16r=Strman.ensureRight("foobar","bar"); Strings17r=Strman.ensureRight("foo","bar"); Strings18r=Strman.ensureRight("fooBAR","bar",false); System.out.println("ensureRight:"+s16r+","+s17r+","+s18r); //result=>"foobar,foobar,fooBAR" //base64Encode将字符串转成Base64编码的字符串 Strings19=Strman.base64Encode("strman"); System.out.println("base64Encode:"+s19);//result=>"c3RybWFu" //binDecode将二进制编码(16位)转成字符串字符 Strings20=Strman.binDecode("0000000001000001"); System.out.println("binDecode:"+s20);//result=>"A" //binEncode将字符串字符转成二进制编码(16位) Strings21=Strman.binEncode("A"); System.out.println("binEncode:"+s21);//result=>"0000000001000001" //decDecode将十进制编码(5位)转成字符串字符 Strings22=Strman.decDecode("00065"); System.out.println("decDecode:"+s22);//result=>"A" //decEncode将字符串转成十进制编码(5位) Strings23=Strman.decEncode("A"); System.out.println("decEncode:"+s23);//result=>"00065" //first得到从字符串开始到索引n的字符串 Strings24=Strman.first("foobar",3); System.out.println("first:"+s24);//result=>"foo" //last得到从字符串结尾倒数索引n的字符串 Strings24l=Strman.last("foobar",3); System.out.println("last:"+s24l);//result=>"bar" //head得到字符串的第一个字符 Strings25=Strman.head("foobar"); System.out.println("head:"+s25);//result=>"f" //hexDecode将字符串字符转成十六进制编码(4位) Strings26=Strman.hexDecode("0041"); System.out.println("hexDecode:"+s26);//result=>"A" //hexEncode将十六进制编码(4位)转成字符串字符 Strings27=Strman.hexEncode("A"); System.out.println("hexEncode:"+s27);//result=>"0041" //inequal测试两个字符串是否相等 booleans28=Strman.inequal("a","b"); System.out.println("inequal:"+s28);//result=>"true" //insert将子字符串插入到字符串某索引位置处 Strings29=Strman.insert("fbar","oo",1); System.out.println("insert:"+s29);//result=>"foobar" //leftPad将字符串从左补齐直到总长度为n为止 Strings30=Strman.leftPad("1","0",5); System.out.println("leftPad:"+s30);//result=>"00001" //rightPad将字符串从右补齐直到总长度为n为止 Strings30r=Strman.rightPad("1","0",5); System.out.println("rightPad:"+s30r);//result=>"10000" //lastIndexOf此方法返回在指定值的最后一个发生的调用字符串对象中的索引,从偏移量中向后搜索 ints31=Strman.lastIndexOf("foobarfoobar","F",false); System.out.println("lastIndexOf:"+s31);//result=>"6" //leftTrim移除字符串最左边的所有空格 Strings32=Strman.leftTrim("strman"); System.out.println("leftTrim:"+s32);//result=>"strman" //rightTrim移除字符串最右边的所有空格 Strings32r=Strman.rightTrim("strman"); System.out.println("rightTrim:"+s32r);//result=>"strman" //removeEmptyStrings移除字符串数组中的空字符串 String[]s33=Strman.removeEmptyStrings(newString[]{"aa","","","bb","cc",null}); System.out.println("removeEmptyStrings:"+Arrays.toString(s33)); //result=>"[aa,bb,cc]" //removeLeft得到去掉前缀(如果存在的话)后的新字符串 Strings34=Strman.removeLeft("foobar","foo"); System.out.println("removeLeft:"+s34);//result=>"bar" //removeRight得到去掉后缀(如果存在的话)后的新字符串 Strings34r=Strman.removeRight("foobar","bar"); System.out.println("removeRight:"+s34r);//result=>"foo" //removeNonWords得到去掉不是字符的字符串 Strings35=Strman.removeNonWords("foo&bar-"); System.out.println("removeNonWords:"+s35);//result=>"foobar" //removeSpaces移除所有空格 Strings36=Strman.removeSpaces("strman"); System.out.println("removeSpaces:"+s36);//result=>"strman" //repeat得到给定字符串和重复次数的新字符串 Strings37=Strman.repeat("1",3); System.out.println("repeat:"+s37);//result=>"111" //reverse得到反转后的字符串 Strings38=Strman.reverse("foobar"); System.out.println("reverse:"+s38);//result=>"raboof" //safeTruncate安全的截断字符串,不切一个字的一半,它总是返回最后一个完整的单词 Strings39=Strman.safeTruncate("AJavascriptstringmanipulationlibrary.",19,"..."); System.out.println("safeTruncate:"+s39);//result=>"AJavascript..." //truncate不太安全的截断字符串 Strings40=Strman.truncate("AJavascriptstringmanipulationlibrary.",19,"..."); System.out.println("truncate:"+s40);//result=>"AJavascriptstr..." //htmlDecode将html字符反转义 Strings41=Strman.htmlDecode("Ш"); System.out.println("htmlDecode:"+s41);//result=>"Ш" //htmlEncode将html字符转义 Strings42=Strman.htmlEncode("Ш"); System.out.println("htmlEncode:"+s42);//result=>"Ш" //shuffle将给定字符串转成随机字符顺序的字符串 Strings43=Strman.shuffle("shekhar"); System.out.println("shuffle:"+s43);//result=>"rhsheak" //slugify将字符串分段(用"-"分段) Strings44=Strman.slugify("foobar"); System.out.println("slugify:"+s44);//result=>"foo-bar" //transliterate删除所有非有效字符,如:á=>a Strings45=Strman.transliterate("fóõbár"); System.out.println("transliterate:"+s45);//result=>"foobar" //surround给定的“前缀”和“后缀”来包裹一个字符串 Strings46=Strman.surround("div","<",">"); System.out.println("surround:"+s46);//result=>"<div>" //tail得到去掉第一个字符后的字符串 Strings47=Strman.tail("foobar"); System.out.println("tail:"+s47);//result=>"oobar" //toCamelCase转成驼峰式的字符串 Strings48=Strman.toCamelCase("CamelCase"); Strings48_2=Strman.toCamelCase("camel-case"); System.out.println("tail:"+s48+","+s48_2);//result=>"camelCase,camelCase" //toStudlyCase转成Studly式的字符串 Strings49=Strman.toStudlyCase("helloworld"); System.out.println("toStudlyCase:"+s49);//result=>"HelloWorld" //toDecamelize转成Decamelize式的字符串 Strings50=Strman.toDecamelize("helloWorld",null); System.out.println("toDecamelize:"+s50);//result=>"helloworld" //toKebabCase转成Kebab式的字符串 Strings51=Strman.toKebabCase("helloWorld"); System.out.println("toKebabCase:"+s51);//result=>"hello-world" //toSnakeCase转成Snake式的字符串 Strings52=Strman.toSnakeCase("helloworld"); System.out.println("toSnakeCase:"+s52);//result=>"hello_world" } }
总结
以上就是这篇文章的全部内容,希望能对大家的学习或者工作带来一定的帮助,如果有疑问大家可以留言交流。