Java by API/org.apache.commons.lang/StringUtils — различия между версиями

Материал из Java эксперт
Перейти к: навигация, поиск
м (1 версия)
 
м (1 версия)
 
(нет различий)

Текущая версия на 14:47, 31 мая 2010

StringUtils: abbreviate(String str, int len)

/*
1) Abbreviate Once upon a time >>>Once upon... * */
import org.apache.rumons.lang.StringUtils;
public class StringUtilsTrial {
  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));
  }
}





StringUtils: chomp(String str, String separator)

/*
3) Chomp END >>>A test String 
 */
import org.apache.rumons.lang.StringUtils;
public class StringUtilsTrial {
  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"));
  }
}





StringUtils: containsNone(String str, String invalidChars)

/*
14) Check that ABCD contains none of !@#$%^&* >>>true
 */
import org.apache.rumons.lang.StringUtils;
public class StringUtilsTrial {
  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", "!@#$%^&*"));
  }
}





StringUtils: containsOnly(String str, String validChars)

/*
4) Check if 643287460 contains only 0123456789 >>>true
 */
import org.apache.rumons.lang.StringUtils;
public class StringUtilsTrial {
  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"));
  }
}





StringUtils: defaultString(String str)

/*
6) Return default string >>>****
 */
import org.apache.rumons.lang.StringUtils;
public class StringUtilsTrial {
  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)
        + "**");
  }
}





StringUtils: equals(String arg0, String arg1)

/*
StringUtils.equals(String arg0, String arg1)
 */
import org.apache.rumons.lang.StringUtils;
public class StringUtilsTrial {
  public static void main(String[] args) {
    // Compare Strings...No NullPointer Exception!
    System.out.println("12) Comapre null and null >>>" + StringUtils.equals(null, null));
  }
}





StringUtils: indexOfDifference(String str1, String str2)

/*
2) Index Of Difference between ABCXYZ and ABCPQR >>>3
 */
import org.apache.rumons.lang.StringUtils;
public class StringUtilsTrial {
  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"));
  }
}





StringUtils: join(Object[] array, String separator)

/*
7) Join Strings using separator >>>AB$#$CD$#$EF
 */
import org.apache.rumons.lang.StringUtils;
public class StringUtilsTrial {
  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" }, "$#$"));
  }
}





StringUtils: reverse(String str)

/*
9) Reverse >>>ESREVER
 */
import org.apache.rumons.lang.StringUtils;
public class StringUtilsTrial {
  public static void main(String[] args) {
    // Reverse a String
    System.out.println("9) Reverse >>>" + StringUtils.reverse("REVERSE"));
  }
}





StringUtils: split(String str, String separatorChars)

/*
0) AB
1) CD
2) EF
3) GH
 * */
import org.apache.rumons.lang.StringUtils;
public class SplitString {
    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]);
        }
    }
}





StringUtils: stripToEmpty(String str)

/*
13) Strip whitespace >>>ABCD
 */
import org.apache.rumons.lang.StringUtils;
public class StringUtilsTrial {
  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      "));
  }
}





StringUtils: substring(String str, int start, int end)

/*
) Substring >>>UBST
 */
import org.apache.rumons.lang.StringUtils;
public class StringUtilsTrial {
  public static void main(String[] args) {

    // SubString
    System.out.println("8) Substring >>>" + StringUtils.substring("SUBSTRING", 1, 5));
  }
}





StringUtils: trim(String str)

/*
10) Trim String. No NullPointerException >>>null
 */
import org.apache.rumons.lang.StringUtils;
public class StringUtilsTrial {
  public static void main(String[] args) {
    // No NullPointer Exception even if null!
    System.out.println("10) Trim String. No NullPointerException >>>" + StringUtils.trim(null));
  }
}





StringUtils: trimToEmpty(String str)

/*
11) Empty String >>><<<
 */
import org.apache.rumons.lang.StringUtils;
public class StringUtilsTrial {
  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) + "<<<");
  }
}