Java Tutorial/Apache Common/StringUtils
Содержание
- 1 SplitString
- 2 String Escape
- 3 StringUtils.abbreviate
- 4 StringUtils.chomp
- 5 StringUtils.containsNone
- 6 StringUtils.containsOnly
- 7 StringUtils.defaultString
- 8 StringUtils.difference
- 9 StringUtils.equals
- 10 StringUtils.indexOfDifference
- 11 StringUtils.join
- 12 StringUtils.reverse
- 13 StringUtils.stripToEmpty
- 14 StringUtils.substring
- 15 StringUtils.trim
- 16 StringUtils.trimToEmpty
SplitString
import org.apache.rumons.lang.StringUtils;
public class MainClass {
public static void main(String[] args) {
//Split a String into an Array using # as seperator.
String [] splitArr=StringUtils.split("AB#CD#EF#GH", "#");
for(int i=0; i<splitArr.length; i++ ) {
System.out.println( i + ") "+ splitArr[i]);
}
}
}
0) AB 1) CD 2) EF 3) GH
String Escape
import org.apache.rumons.lang.StringEscapeUtils;
public class MainClass {
public static void main(String[] args) {
String strHTMLInput = "<P>MyName<P>";
String strEscapeHTML = StringEscapeUtils.escapeHtml(strHTMLInput);
String strUnEscapeHTML = StringEscapeUtils.unescapeHtml(strEscapeHTML);
System.out.println("Escaped HTML >>> " + strEscapeHTML);
System.out.println("UnEscaped HTML >>> " + strUnEscapeHTML);
}
}
StringUtils.abbreviate
import org.apache.rumons.lang.StringUtils;
public class MainClass {
public static void main(String[] args) {
//String can be max 12 chars including the ...
System.out.println("1) Abbreviate Once upon a time >>>" +
StringUtils.abbreviate("Once upon a time ", 12));
}
}
1) Abbreviate Once upon a time >>>Once upon...
StringUtils.chomp
import org.apache.rumons.lang.StringUtils;
public class MainClass {
public static void main(String[] args) {
//Remove the specified string at the end of String
System.out.println("3) Chomp END >>>" +
StringUtils.chomp("A test String END", "END"));
}
}
3) Chomp END >>>A test String
StringUtils.containsNone
import org.apache.rumons.lang.StringUtils;
public class MainClass {
public static void main(String[] args) {
//Check that a string does not contain any of these characters !@#$%^&*
System.out.println("14) Check that ABCD contains none of !@#$%^&* >>>" +
StringUtils.containsNone("ABCD", "!@#$%^&*"));
}
}
14) Check that ABCD contains none of !@#$%^&* >>>true
StringUtils.containsOnly
import org.apache.rumons.lang.StringUtils;
public class MainClass {
public static void main(String[] args) {
//Check if string contains only a specified set of characters. Return boolean
System.out.println("4) Check if 643287460 contains only 0123456789 >>>" +
StringUtils.containsOnly("643287460", "0123456789"));
}
}
4) Check if 643287460 contains only 0123456789 >>>true
StringUtils.defaultString
import org.apache.rumons.lang.StringUtils;
public class MainClass {
public static void main(String[] args) {
//Takes Object input and returns Empty String if null.
System.out.println("6) Return default string >>>" + "**" +
StringUtils.defaultString(null) + "**");
}
}
6) Return default string >>>****
StringUtils.difference
import org.apache.rumons.lang.StringUtils;
public class MainClass {
public static void main(String[] args) {
//Compare two strings. Case Sensitive
System.out.println("5) Compare ABCDEFG and ABCdefg >>>" +
StringUtils.difference("ABCDEFG", "ABCdefg"));
}
}
5) Compare ABCDEFG and ABCdefg >>>defg
StringUtils.equals
import org.apache.rumons.lang.StringUtils;
public class MainClass {
public static void main(String[] args) {
//Compare Strings...No NullPointer Exception!
System.out.println("12) Comapre null and null >>>" +
StringUtils.equals(null, null));
}
}
12) Comapre null and null >>>true
StringUtils.indexOfDifference
import org.apache.rumons.lang.StringUtils;
public class MainClass {
public static void main(String[] args) {
//Returns index where the Strings start to differ
System.out.println(
"2) Index Of Difference between ABCXYZ and ABCPQR >>>" +
StringUtils.indexOfDifference("ABCXYZ", "ABCPQR"));
}
}
2) Index Of Difference between ABCXYZ and ABCPQR >>>3
StringUtils.join
import org.apache.rumons.lang.StringUtils;
public class MainClass {
public static void main(String[] args) {
//Join all Strings in the Array into a Single String, separated by $#$
System.out.println("7) Join Strings using separator >>>" +
StringUtils.join(new String[] { "AB", "CD", "EF" }, "$#$"));
}
}
7) Join Strings using separator >>>AB$#$CD$#$EF
StringUtils.reverse
import org.apache.rumons.lang.StringUtils;
public class MainClass {
public static void main(String[] args) {
//Reverse a String
System.out.println("9) Reverse >>>" + StringUtils.reverse("REVERSE"));
}
}
9) Reverse >>>ESREVER
StringUtils.stripToEmpty
import org.apache.rumons.lang.StringUtils;
public class MainClass {
public static void main(String[] args) {
//Strip whitespace from start and end of the string.
//If null returns empty string
System.out.println("13) Strip whitespace >>>" +
StringUtils.stripToEmpty(" ABCD "));
}
}
13) Strip whitespace >>>ABCD
StringUtils.substring
import org.apache.rumons.lang.StringUtils;
public class MainClass {
public static void main(String[] args) {
//SubString
System.out.println("8) Substring >>>" +
StringUtils.substring("SUBSTRING", 1, 5));
}
}
8) Substring >>>UBST
StringUtils.trim
import org.apache.rumons.lang.StringUtils;
public class MainClass {
public static void main(String[] args) {
//No NullPointer Exception even if null!
System.out.println("10) Trim String. No NullPointerException >>>" +
StringUtils.trim(null));
}
}
10) Trim String. No NullPointerException >>>null
StringUtils.trimToEmpty
import org.apache.rumons.lang.StringUtils;
public class MainClass {
public static void main(String[] args) {
//If string is null, empty string returned. Else returns trimmed string
System.out.println("11) Empty String >>>" +
StringUtils.trimToEmpty(null) + "<<<");
}
}
11) Empty String >>><<<