You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by ba...@apache.org on 2002/06/04 17:46:57 UTC

cvs commit: jakarta-commons-sandbox/lang/src/java/org/apache/commons/lang Strings.java

bayard      2002/06/04 08:46:57

  Modified:    lang/src/java/org/apache/commons/lang Strings.java
  Log:
  Null protection added to the strip methods. Also a bug fixed in which
  the strip methods would not handle "    ".
  Added a stripAll method which runs strip on each element in an array.
  
  Revision  Changes    Path
  1.4       +42 -10    jakarta-commons-sandbox/lang/src/java/org/apache/commons/lang/Strings.java
  
  Index: Strings.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/lang/src/java/org/apache/commons/lang/Strings.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- Strings.java	23 Apr 2002 13:34:01 -0000	1.3
  +++ Strings.java	4 Jun 2002 15:46:57 -0000	1.4
  @@ -85,7 +85,7 @@
    * @author <a href="mailto:gcoladonato@yahoo.com">Greg Coladonato</a>
    * @author <a href="mailto:bayard@generationjava.com">Bayard</a>
    * @author <a href="mailto:ed@apache.org">Ed Korthof</a>
  - * @version $Id: Strings.java,v 1.3 2002/04/23 13:34:01 bayard Exp $
  + * @version $Id: Strings.java,v 1.4 2002/06/04 15:46:57 bayard Exp $
    */
   public class Strings
   {
  @@ -1067,6 +1067,30 @@
       }
   
       /**
  +     * Strip whitespace from the front and back of every string
  +     * in the array.
  +     */
  +    static public String[] stripAll(String[] strs) {
  +        return stripAll(strs, null);
  +    }
  + 
  +    /**
  +     * Strip the specified delimiter from the front and back of
  +     * every String in the array.
  +     */
  +    static public String[] stripAll(String[] strs, String delimiter) {
  +        if( (strs == null) || (strs.length == 0) ) {
  +            return strs;
  +        }
  +        int sz = strs.length;
  +        String[] newArr = new String[sz];
  +        for(int i=0; i<sz; i++) {
  +            newArr[i] = strip(strs[i], delimiter);
  +        }
  +        return newArr;
  +    }   
  +
  +    /**
        * Swaps the case of String. Properly looks after 
        * making sure the start of words are Titlecase and not 
        * Uppercase.
  @@ -1148,18 +1172,20 @@
        * Strip any of a supplied substring from the end of a String..
        */
       static public String stripEnd(String str, String ch) {
  +        if(str == null) {
  +            return null;
  +        }
           int end = str.length();
  -
  + 
           if(ch == null) {
  -            while( Character.isWhitespace( str.charAt(end-1) ) ) {
  -                end--;
  +            while( (end != 0) && Character.isWhitespace( str.charAt(end-1) ) ) {                end--;
               }
           } else {
               char chr = ch.charAt(0);
  -            while( str.charAt(end-1) == chr ) {
  +            while( (end != 0) && (str.charAt(end-1) == chr) ) {
                   end--;
               }
  -        }
  +        }      
           return str.substring(0, end);
       }
   
  @@ -1167,19 +1193,25 @@
        * Strip any of a supplied substring from the start of a String..
        */
       static public String stripStart(String str, String ch) {
  +        if(str == null) {
  +            return null;
  +        }
  + 
           int start = 0;
  -
  + 
  +        int sz = str.length();
  + 
           if(ch == null) {
  -            while( Character.isWhitespace( str.charAt(start) ) ) {
  +            while( (start != sz) && Character.isWhitespace( str.charAt(start) ) ) {
                   start++;
               }
           } else {
               char chr = ch.charAt(0);
  -            while( str.charAt(start) == chr ) {
  +            while( (start != sz) && (str.charAt(start) == chr ) ) {
                   start++;
               }
           }
  -        return str.substring(start);
  +        return str.substring(start);     
       }
   
       /**
  
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>