Example of the use of string processing library Strman-java in Java8 _java

Source: Internet
Author: User
Tags arrays shuffle truncated

Introduced

Strmen-java is a string processing tool that you can introduce into your project through MAVEN. Strmen-java provides us with a very complete and powerful solution that can be used to solve almost any string processing scenario.

Use

To be able to use Strman-java in your Java application, you can download the package to the Lib directory of your project, and if you are using MAVEN for project management, just add the following dependencies to your pom.xml list:

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

If it is a gradle user, build.gradle Add the following code to the file:

Compile (group: ' Com.shekhargulati ', Name: ' Strman ', version: ' 0.2.0 ', ext: ' jar ') { 
 transitive=true
}

Example

The following is an example of the use of Strman-java:

Import Strman. 
Strman; 
Import Java.util.Arrays;

Import java.util.Optional;
 /** * Strman-java Package Test uses class * Created by Blinkfox on 16/7/17. * * Public class Strmantest {public static void main (string[] args) {//Append append any number of string strings after a string S1 = Strman
  . Append ("F", "O", "O", "B", "A", "R"); System.out.println ("Append:" + S1);
  Result =&gt; "Foobar"//prepend append any number of strings before a string s1pre = Strman.prepend ("R", "F", "O", "O", "B", "a"); System.out.println ("prepend:" + s1pre); Result =&gt; "Foobar"//Appendarray appends a string to an element string in an array of strings S2 = Strman.appendarray ("f", New string[]{"O",
  "O", "B", "A", "R"}); System.out.println ("Append:" + s2); Result =&gt; "Foobar"//at the corresponding character according to the index of the string.
  If the index is a negative number, then the reverse gets, the exception throws the anomaly optional&lt;string&gt; s3 = strman.at ("Foobar", 3); System.out.println ("at:" + s3.get ());
  Result =&gt; "B"//Between gets an array of strings between the start and end strings string[] S4 = Strman.between ("[ABC], [Def]", "[", "]"); System.out.println ("BetweeN: "+ arrays.tostring (S4));
  Result =&gt; "[ABC, DEF]"///Chars get a string array of all the characters in a string string[] S5 = Strman.chars ("title"); System.out.println ("chars:" + arrays.tostring (S5)); Result =&gt; "[t, I, T, L, E]"//Collapsewhitespace replaces consecutive multiple spaces as a space String s6 = strman.collapsewhitespace ("foo BA
  R "); System.out.println ("chars:" + S6); Result =&gt; "foo bar"//contains determines whether a string contains another string, and the third argument indicates whether the string case is sensitive Boolean s7 = Strman.contains ("foo bar", "
  Foo ");
  Boolean S8 = Strman.contains ("foo bar", "foo", false); System.out.println ("contains:" + s7 + "," + S8); Result =&gt; "true, true"//Containsall determines whether a string contains all elements of a string array boolean S9 = Strman.containsall ("foo bar", New St
  ring[]{"foo", "Bar"});
  Boolean s10 = Strman.containsall ("foo bar", New string[]{"foo", "Bar"}, False); System.out.println ("Containsall:" + S9 + "," + S10);  Result =&gt; "true, true"//Containsany determines whether a string contains any of the elements in a string array boolean S11 = Strman.containsany ("foo bar", new string[]{"FOO", "BAR", "Test"}, False); System.out.println ("Containsany:" + S11);
  Result =&gt; "true"//COUNTSUBSTR determines the number of a string containing a string long S12 = Strman.countsubstr ("Aaaaaaaaa", "AAA");
  Long S13 = Strman.countsubstr ("Aaaaaaaaa", "AAA", false, false); System.out.println ("Countsubstr:" + S12 + "," + S13);
  Result =&gt; "2, 3"//EndsWith determines whether a string ends with a string of Boolean S14 = Strman.endswith ("foo bar", "Bar");
  Boolean S15 = Strman.endswith ("foo bar", "bar", false); System.out.println ("EndsWith:" + S14 + "," + S15); Result =&gt; "true, true"//ensureleft ensure that a string begins with a string, and if not, appends the string in front of it, and returns the string results to 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 =&gt; "Foobar, Foobar, foobar"//ensureright ensure that a string begins with a string and, if not, appends the string in front of it, and returns the string results to string s16r = Strma
 N.ensureright ("Foobar", "Bar"); String s17r = strman.ensureright ("foo", "Bar");
  String s18r = strman.ensureright ("FooBAR", "bar", false);
  System.out.println ("Ensureright:" + s16r + "," + s17r + "," + s18r); Result =&gt; "Foobar, Foobar, foobar"//Base64Encode converts a string to a Base64-encoded string s19 = Strman.base64encode ("Strman"
  ); System.out.println ("Base64Encode:" + s19);
  Result =&gt; "C3RYBWFU"///Bindecode converts the binary encoding (16-bit) to a string of characters, S20 = Strman.bindecode ("0000000001000001"); System.out.println ("Bindecode:" + S20);
  Result =&gt; "a"//Binencode converts string characters into binary encodings (16-bit) string S21 = Strman.binencode ("a"); System.out.println ("Binencode:" + S21);
  Result =&gt; "0000000001000001"//Decdecode converts the decimal encoding (5-bit) to string-character strings S22 = Strman.decdecode ("00065"); System.out.println ("Decdecode:" + S22);
  Result =&gt; "a"//Decencode converts the string to decimal encoding (5-bit) string s23 = Strman.decencode ("a"); System.out.println ("Decencode:" + s23); Result =&gt; ' 00065 '//First gets a string of strings S24 = S from the beginning of the string to the index nTrman.first ("Foobar", 3); System.out.println ("A:" + S24);
  Result =&gt; ' foo '//Last Gets the string s24l = Strman.last ("Foobar", 3) from the reciprocal index of the end of the string. System.out.println ("Last:" + s24l);
  Result =&gt; the "bar"//head Gets the string's first character string s25 = Strman.head ("Foobar"); System.out.println ("head:" + S25);
  Result =&gt; "F"//Hexdecode converts string characters to hexadecimal-encoded (4-bit) string S26 = Strman.hexdecode ("0041"); System.out.println ("Hexdecode:" + S26);
  Result =&gt; "a"//Hexencode converts the hexadecimal encoding (4-bit) to a string character, S27 = Strman.hexencode ("a"); System.out.println ("Hexencode:" + s27);
  Result =&gt; "0041"//Inequal tests whether two strings are equal Boolean S28 = Strman.inequal ("A", "B"); System.out.println ("inequal:" + s28);
  Result =&gt; "true"//Insert inserts a substring at an index of the string s29 = Strman.insert ("Fbar", "oo", 1); System.out.println ("Insert:" + s29);
  Result =&gt; "Foobar"//Leftpad the string from left until the total length is n string s30 = Strman.leftpad ("1", "0", 5); System.out.println ("Leftpad:" + S30);Result =&gt; "00001"//Rightpad the string from right until the total length is n string s30r = Strman.rightpad ("1", "0", 5); System.out.println ("Rightpad:" + s30r); Result =&gt; "10000"//LastIndexOf This method returns the index in the call string object that occurred at the last occurrence of the specified value, and searches backwards from the offset for the int s31 = Strman.lastindexof ("Foobarfoo
  Bar "," F ", false); System.out.println ("LastIndexOf:" + s31);
  Result =&gt; "6"//Lefttrim removes all space string s32 = Strman.lefttrim ("Strman") to the left of the strings; System.out.println ("Lefttrim:" + S32);
  Result =&gt; "Strman"//Righttrim removes all space strings at the far right of the string s32r = Strman.righttrim ("Strman"); System.out.println ("Righttrim:" + s32r); Result =&gt; "Strman"//Removeemptystrings removes an empty string from an array of strings string[] S33 = strman.removeemptystrings (new string[]{)
  AA "," "," "," BB "," CC ", null});
  System.out.println ("removeemptystrings:" + arrays.tostring (S33));
  Result =&gt; "[AA, BB, CC]"///Removeleft gets rid of the prefix (if present) after the new string s34 = Strman.removeleft ("Foobar", "foo"); System.out.println ("Removeleft:" + s34); // Result =&gt; "Bar"//removeright gets rid of the suffix (if present) after the new string s34r = Strman.removeright ("Foobar", "Bar"); System.out.println ("Removeright:" + s34r);
  Result =&gt; "foo"//Removenonwords get rid of strings that are not characters string s35 = Strman.removenonwords ("foo&amp;bar-"); System.out.println ("removenonwords:" + S35);
  Result =&gt; "Foobar"//removespaces Remove all space String S36 = strman.removespaces ("str man"); System.out.println ("removespaces:" + s36);
  Result =&gt; "Strman"//Repeat gets a new string with a given string and number of repetitions, string s37 = Strman.repeat ("1", 3); System.out.println ("repeat:" + s37);
  Result =&gt; "a"//reverse Gets the inverted string s38 = Strman.reverse ("Foobar"); System.out.println ("Reverse:" + s38); Result =&gt; "Raboof"//safetruncate Secure truncated string, not half a word, it always returns the last complete word string s39 = Strman.safetruncate ("a JAVASCR
  IPT string manipulation Library. ", 19," ... "); System.out.println ("safetruncate:" + s39); Result =&gt; "A Javascript ..."//truncate a less secure truncated string string S40 = strman.truncate ("A Javascript string Manipulation Library.", 19, "..."); SYSTEM.OUT.PRINTLN ("Truncate:" + S40);

  Result =&gt; "A Javascript str ..."
  HtmlDecode the HTML character by inverting the literal String s41 = Strman.htmldecode ("ш"); System.out.println ("HtmlDecode:" + s41);
  Result =&gt; "ш"//HtmlEncode HTML word escape string s42 = Strman.htmlencode ("ш"); System.out.println ("HtmlEncode:" + s42);
  Result =&gt; "ш"//Shuffle A string that converts a given string to a random character order s43 = Strman.shuffle ("Shekhar"); System.out.println ("Shuffle:" + s43);
  Result =&gt; "Rhsheak"//Slugify string (with "-") string s44 = Strman.slugify ("foo bar"); System.out.println ("slugify:" + s44);
  Result =&gt; "Foo-bar"//transliterate deletes all non valid characters, such as:á=&gt; a String S45 = Strman.transliterate ("Fóõbár"); System.out.println ("transliterate:" + s45);
  Result =&gt; "foo bar"//surround given "prefix" and "suffix" to wrap a string s46 = Strman.surround ("div", "&lt;", "&gt;"); System.out.println ("Surround:" + s46); ResulT =&gt; "&lt;div&gt;"//Tail get rid of the first character string s47 = Strman.tail ("Foobar"); System.out.println ("tail:" + s47);
  Result =&gt; "Oobar"//Tocamelcase into a hump-style string of strings S48 = Strman.tocamelcase ("Camel case");
  String s48_2 = strman.tocamelcase ("Camel-case"); System.out.println ("tail:" + S48 + "," + s48_2); 
  Result =&gt; "CamelCase, CamelCase"//Tostudlycase converted to a studly-style string of strings S49 = strman.tostudlycase ("Hello World"); System.out.println ("tostudlycase:" + s49);
  Result =&gt; "HelloWorld"//Todecamelize to Decamelize-type string S50 = Strman.todecamelize ("HelloWorld", null); System.out.println ("todecamelize:" + S50);
  Result =&gt; the "Hello World"//Tokebabcase into a kebab-style string s51 = Strman.tokebabcase ("Hello World"); System.out.println ("tokebabcase:" + s51);
  Result =&gt; "Hello-world"//Tosnakecase into a snake-style string of strings S52 = strman.tosnakecase ("Hello World"); System.out.println ("tosnakecase:" + s52); Result =&gt; "Hello_world"
 }

} 

Summarize

The above is the entire content of this article, I hope to be able to learn or work to bring certain help, if there is doubt you can message exchange.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.