Examples of usages of org.apache.commons.lang.StringUtils

Source: Internet
Author: User
Tags array length
Package randomstringutils;


Import Org.apache.commons.lang.StringUtils;


public class Stringutilstest {
public static void Main (string[] args) {
/* 1. String starts with prefix
System.out.println ("-------------1-----------------");
System.out.println (Stringutils.startswith ("Sssdf", ""));//The result is: true
System.out.println (Stringutils.startswith ("Sssdf", ""));//The result is: true
System.out.println (Stringutils.startswith ("Sssdf", "s"));//The result is: true
System.out.println (Stringutils.startswith ("Sssdf", "SS"));//The result is: true
System.out.println (Stringutils.startswith ("Sssdf", "SSS"));//The result is: true
System.out.println (Stringutils.startswith ("Sssdf", "SSSDF"));//The result is: true
System.out.println (Stringutils.startswith ("Sssdf", "F"));//The result is: false
System.out.println (Stringutils.startswith ("Sssdf", "SSSDF"));//The result is: true


System.out.println ("-------------2-----------------");
/* 2. String starts with prefix, case-insensitive
System.out.println (Stringutils.startswithignorecase ("Sssdf", "SSSDF"));//The result is: true
System.out.println ("-------------3-----------------");
/* 3. The string starts with a string in the array.
System.out.println (Stringutils.startswithany) ("Aabcde", new
String[]{"G", "F"});//The result is: false
System.out.println (Stringutils.startswithany) ("Aabcde", new
String[]{"G", "a"});//The result is: true
System.out.println ("------------------------------");
/* 4. String ended with suffix/*
System.out.println (Stringutils.endswith ("Aabcde", "D"));//The result is: false
System.out.println (Stringutils.endswith ("Aabcde", "E"));//The result is: true
System.out.println (Stringutils.endswithignorecase ("", ""));//The result is: true
System.out.println ("-------------4-----------------");
* 5. Replacement string: Replace the searchstring in text with the Replacement,max is the maximum number of replacements, by default, replace all the */
System.out.println (Stringutils.replaceonce ("Sshhhss", "SS", "P"));//Replace only once--> the result is: PHHHSS
System.out.println (Stringutils.replace ("SSHHHS", "SS", "P"))//Replace All---> Result: PHHHS
System.out.println (Stringutils.replace ("Sshhhsshss", "ss", "7777", 2))/MAX: Max replacement number--> result: 7777HHH7777HSS
System.out.println (Stringutils.replacechars ("SSHHHS", "SS", "P"));//Replace all characters, different from replace---> Result: pphhhp instead of PPHHHS
System.out.println ("--------------5----------------");
/*
* 6. Replace according to array, position to match, array length is equal; I don't see the difference between Replaceeach and replaceeachrepeatedly.
*/
System.out.println (Stringutils.replaceeach ("www.baidu.com"),
New string[] {"Baidu", "com"},
New string[] {"Taobao", "NET"});//The result is: www.taobao.net
System.out.println (Stringutils.replaceeach ("www.baidu,baidu.com"),
New string[] {"Baidu", "com"},
New string[] {"Taobao", "NET"});//The result is: www.taobao,taobao.net
System.out.println (stringutils.replaceeachrepeatedly ("www.baidu.com"),
New string[] {"Baidu", "com"},
New string[] {"Taobao", "NET"});//The result is: www.taobao.net
System.out.println (stringutils.replaceeachrepeatedly (
"Www.baidu,baidu.com", new string[] {"Baidu", "com"},
New string[] {"Taobao", "NET"});//The result is: www.taobao.net
System.out.println ("--------------6----------------");
/* 7. Compare two strings for equality, and if two are null, also consider equal * *
System.out.println (Stringutils.equals ("", ""));//The result is true
SYSTEM.OUT.PRINTLN (stringutils.equals (null, NULL));//result is true
System.out.println (Stringutils.equals ("", null));//The result is false
SYSTEM.OUT.PRINTLN (Stringutils.equals (null, ""));//The result is false
System.out.println (Stringutils.equalsignorecase ("ss", "SS"));//case-insensitive-the result is true
System.out.println ("--------------7----------------");


/* 8. Returns the position of the first occurrence of the SearchChar in the string, or 1 if the SearchChar is not present in the string.
System.out.println (Stringutils.indexof ("SDFSFSFDSF", "4"))/* result is-1 *
System.out.println (Stringutils.indexof ("SDFSFSFDSF", "F"));/* result is 2 */
System.out.println ("--------------8----------------");
Find the index where SearchChar last appeared in the string * *
System.out.println (Stringutils.lastindexof ("Afkyk", "K"));/The result is 4.
System.out.println (Stringutils.lastindexof ("Afkyk", ""));/The result is 5.
System.out.println ("--------------9----------------");
/* 9. Find out where the character array Searchars first appears in the string * *
System.out.println (Stringutils.indexofany ("Sdsfhhl0", "F"));/The result is 3.
System.out.println (Stringutils.indexofany ("Sdsfhhl0", new string[) {
"F", "0"});//The result is 3.
System.out.println (Stringutils.indexofany ("Sdsfhhl0", new string[) {
"T", "J"});/The result is-1
System.out.println (Stringutils.indexofany ("Sdsfhhl0", new string[) {
"T", "J", ""});/The result is 3.
System.out.println (Stringutils.lastindexofany ("Afkyk", new string[) {
"Afkyk", "K"});/find the position where the character array Searchars last appeared in the string-the result is 4
System.out.println ("------------------------------");


/*
* 10. Find the position of the first character in the string that is not in the character array searchars (starting with 0 bits) if all is in, return-1
*/
System.out.println (Stringutils.indexofanybut ("Sdsfhhl0", "H"));/The result is 0.
System.out.println (Stringutils.indexofanybut ("Sdsfhhl0", "s"));/The result is 1.
System.out.println (stringutils.indexofanybut ("AA", "AA"));/The result is-1
System.out.println ("------------------------------");
/* 11. Statistics Parameters 1 and the beginning of parameter 2 the number of characters in common * *
System.out.println (Stringutils.indexofdifference ("SDSFDSF", "s"));/The result is 1.
System.out.println (Stringutils.indexofdifference (new string[] {"SDSFDSF", "s"}));/The result is 1.
System.out.println ("------------------------------");
* 12. Remove the string that is shared by parameter 2 at the beginning of parameter 1.
System.out.println (Stringutils.difference ("irrigation, irrigation, irrigation, irrigation and irrigation)");/The result is: Ah Ah
System.out.println ("------------------------------");
* 13. Find, case-insensitive, not found return-1 * *
System.out.println (Stringutils.indexofignorecase ("AFABBSSDD", "F"));/return 1
System.out.println (Stringutils.indexofignorecase ("AFABBSSDD", "F", 2))//find from specified position, case-insensitive--return-1
System.out.println (Stringutils.indexofignorecase ("AFABBSSDD", "F", 1));/return 1
System.out.println (Stringutils.lastindexofignorecase ("", ""));
System.out.println (Stringutils.lastindexofignorecase ("", "", 2));


/* 14. Intercept the string at the specified position * *
System.out.println (stringutils.substring ("Dskabcee", 3));
/* The result is: Abcee * *
System.out.println ("------------------------------");
System.out.println (stringutils.substring ("Dskabcee", 3, 5));
/* The result is: AB * *
System.out.println ("------------------------------");
* 15. Intercept the content before the specified string.
System.out.println (Stringutils.substringbefore ("Dskeabcee", "E"));
/* The result is: DSKEABCE * *
System.out.println ("------------------------------");
System.out.println (Stringutils.substringbeforelast ("Dskeabcee", "E"))//always finds the last specified string
/* The result is: DSKEABCE * *
System.out.println ("------------------------------");
System.out.println (Stringutils.substringafter ("Dskeabcedeh", ""));
/* The result is: Dskeabcedeh * *
System.out.println ("------------------------------");
System.out.println (Stringutils.substringafterlast ("Dskeabcedeh", ""));
/* The result is: * *
System.out.println ("------------------------------");
* 16. Intercept the characters in the middle of parameter 2 and parameter 3 * *
System.out.println (Stringutils.substringbetween ("Dskeabcedeh", "DS"));
/* The result is: null */
System.out.println (Stringutils.substringbetween) ("Dskeabcedeh", "DS",
"E"));
/* The result is: k/*
System.out.println (Stringutils.substringsbetween) ("Dskeabcedeh", "DS",
"E")//returns the string in the middle of parameter 2 and parameter 3 as an array
/* The result is: [KEABCE] * *
System.out.println ("------------------------------");
/* 1. Split string, you can set the length of the array, but generally do not set, this will occur conflict
System.out.println (Stringutils.split ("y5y,4454,545"));//default is by, to split
System.out.println (Stringutils.split ("Aaaa#sss", "#"));
/* The result is: [AAAA,SSS] * *
System.out.println ("------------------------------");
System.out.println (Stringutils.split ("aaaa#sss#", "#", 2));
System.out.println ("------------------------------");
/* 2. Split string by different type
System.out.println (Stringutils.splitbycharactertype ("aa3444 John Bcss"));
/* The result is: [aa,3444, John, A,b,css,b] * *
System.out.println ("------------------------------");
System.out.println (Stringutils.splitbycharactertypecamelcase (""));
/* The result is: [aa,3444, John, A,bcss,b] * *
System.out.println ("------------------------------");
/* 3. Split string, "" will not be ignored, you can set the length of the array of the split string * *
System.out.println (Stringutils.splitbywholeseparator ("aaaa#sss#", "#"));//"" will not be ignored
/* The result is: [Aaaa,sss,]system.out.println (stringutils.split result: [AAAA,SSS] * *
System.out.println ("------------------------------");
System.out.println (Stringutils.splitbywholeseparator ("AAAA#SSS#GGG"),
"#"));//
/* The result is: [AAAA,SSS,GGG] * *
System.out.println ("------------------------------");
System.out.println (stringutils
. Splitbywholeseparator ("Aaaa#sss#ggg", ""))//By Space
/* The result is: [AAAA#,SSS#GGG] * *
System.out.println ("------------------------------");
System.out.println (Stringutils.splitbywholeseparator) ("aaaa#sss#", "#",
2);//2 sets the maximum length of the returned array
/* The result is: [aaaa,sss#] * *
System.out.println ("------------------------------");
/* 4. Split string, "" will not be ignored, you can set the length of the array of the split string * *
System.out.println (Stringutils.splitbywholeseparatorpreservealltokens (
"Sddssfsfasfsaf", null));
/* The result is: [Sddssf,sfasfsaf,] * *
System.out.println ("------------------------------");
System.out.println (Stringutils.splitbywholeseparatorpreservealltokens (
"Sddssfsfasfsaf", ""));
/* The result is: [Sddssf,sfasfsaf,] * *
System.out.println ("------------------------------");
/* 5 Ditto * *
System.out.println (Stringutils.splitpreservealltokens (""));
System.out.println (Stringutils.splitpreservealltokens ("", ""));
System.out.println (Stringutils.splitpreservealltokens ("", "", 3));
}

}



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.