2008年1月6日日曜日

[Java>Original]Useful String Utility(Utility to use String Object)

String Utility, converter, check routine, and compare method to avoid NullPointerException or other Exception.

References
  • http://math.hws.edu/eck/cs124/javanotes4/c4/ex-4-1-answer.html
  • http://forum.java.sun.com/thread.jspa?threadID=442797&messageID=2013116
Original Source Code
  • http://taapps-sourcecode-libraries.googlegroups.com/web/Str.java
  • http://taapps-sourcecode-libraries.googlegroups.com/web/taapps-bnr-0.2.jar
Methods
  • Convert first letter of words to capitalized letter.
    String testStr3=Str.getCapitalizedFirstLetter("capitalIZED fiRSt leTTER");
    Output of testStr3 object is "Capitalized First Letter"
  • Compare 2 String objects
    Compare 2 String objects and check whether the value of String object are the same or not. You don't need to check null of String object before calling this method.
    Third argument compare 2 strings ignore case
    String test1="Test1";
    String test2="test1";
    //case sensitive
    boolean result=Str.isSame(test1,test2,false));
    //result is false

    //ignore case Pattern
    result=Str.isSame(test1,test2,true));
    //result is true because comparing 2 string by using equalsIgnoreCase function
  • nvl(return the second object if the first argument object is null)
    Function return the second argument object if the first argument is null. This function works like ORACLE PL/SQL's nvl function.
    String test1=null;
    String test2="test1";
    String test3=Str.nvl(test1,test2);
    //test3 is "test1" because test1 object is null
    String test4=Str.nvl(test3,test2);
    //test4 is same as test3 object
  • padding and format number
    //right padding with @ char
    String paddingRight=Str.padStr("12345",20,'@',Str.NUMBER_PADDING_RIGHT);
    //Output is 12345@@@@@@@@@@@@@@@
    //left padding with X char
    String paddingLeft =Str.padStr("12345",20,'X',Str.NUMBER_PADDING_LEFT);
    //Output is XXXXXXXXXXXXXXX12345
  • padding with 0
    String padTest1=Str.padWithZero(12345,20);
    //Output of padTest1 String is 00000000000000012345
  • indexOf
    String test1="Test1";
    String test2="test1";
    boolean result=Str.indexOf(test1,test2,false));
    //result is false
    boolean result=Str.indexOf(test1,test2,true));
    //result is true
  • split string
    String test1="test1,test2,test3,test4,test5";
    String[] splittedStrs=Str.splitStr(test1,",")
  • Check whether the value of second argument is being included into the first argument array or not. Third argument is ignorecase flag.
    String[] array=new String[]{"test1","test2","test3","test4"};
    boolean result=Str.checkContain(array,"TEST3",false);
    //result is false

    //ignore case pattern
    boolean result=Str.checkContain(array,"TEST3",true);
    //result is true
  • Concatenate string array with second argument separator string
    String[] array=new String[]{"test1","test2","test3","test4"};
    String concat=Str.concat(array,"--");
    Output of concat Object is test1--test2--test3--test4
  • Check Utility(String length and format)
    • checkLength
    • checkCharsExpression
    • checkLengthAndExpression
  • URL encoder and decoder
    • decodeUrl
      encodeUrl

0 件のコメント: