commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From scolebou...@apache.org
Subject cvs commit: jakarta-commons-sandbox/lang/src/java/org/apache/commons/lang Strings.java
Date Tue, 02 Jul 2002 00:00:15 GMT
scolebourne    2002/07/01 17:00:14

  Modified:    lang/src/java/org/apache/commons/lang Strings.java
  Log:
  Add left, right and mid methods
  
  Revision  Changes    Path
  1.9       +76 -1     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.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- Strings.java	1 Jul 2002 20:59:07 -0000	1.8
  +++ Strings.java	2 Jul 2002 00:00:14 -0000	1.9
  @@ -236,6 +236,81 @@
       }
   
       /**
  +     * Get the leftmost n characters of a string. If n characters are not 
  +     * available, or the string is null, the string will be returned 
  +     * without an exception.
  +     *
  +     * @param str  the string to get the leftmost characters from
  +     * @param len  the length of the required string
  +     * @return the leftmost characters
  +     * @throws IllegalArgumentException if len is less than zero
  +     */
  +    public static String left(String str, int len)
  +    {
  +        if (len < 0) {
  +            throw new IllegalArgumentException("Requested String length " + len + " is
less than zero");
  +        }
  +        if ((str == null) || (str.length() <= len)) {
  +            return str;
  +        } else {
  +            return str.substring(0, len);
  +        }
  +    }
  +
  +    /**
  +     * Get the rightmost n characters of a string. If n characters are not 
  +     * available, or the string is null, the string will be returned 
  +     * without an exception.
  +     *
  +     * @param str  the string to get the rightmost characters from
  +     * @param len  the length of the required string
  +     * @return the leftmost characters
  +     * @throws IllegalArgumentException if len is less than zero
  +     */
  +    public static String right(String str, int len)
  +    {
  +        if (len < 0) {
  +            throw new IllegalArgumentException("Requested String length " + len + " is
less than zero");
  +        }
  +        if ((str == null) || (str.length() <= len)) {
  +            return str;
  +        } else {
  +            return str.substring(str.length() - len);
  +        }
  +    }
  +
  +    /**
  +     * Get n characters from the middle of a string. If n characters are 
  +     * not available, the remainder of the string will be returned 
  +     * without an exception. If the string is null, null will be returned.
  +     *
  +     * @param str  the string to get the characters from
  +     * @param pos  the position to start from
  +     * @param len  the length of the required string
  +     * @return the leftmost characters
  +     * @throws IndexOutOfBoundsException if pos is out of bounds
  +     * @throws IllegalArgumentException if len is less than zero
  +     */
  +    public static String mid(String str, int pos, int len)
  +    {
  +        if ((pos < 0) ||
  +            (str != null && pos > str.length())) {
  +            throw new StringIndexOutOfBoundsException("String index " + pos + " is out
of bounds");
  +        }
  +        if (len < 0) {
  +            throw new IllegalArgumentException("Requested String length " + len + " is
less than zero");
  +        }
  +        if (str == null) {
  +            return null;
  +        }
  +        if (str.length() <= (pos + len)) {
  +            return str.substring(pos);
  +        } else {
  +            return str.substring(pos, pos + len);
  +        }
  +    }
  +
  +    /**
        * @see #split(String text, String separator, int max)
        */
       public static String[] split(String text)
  
  
  

--
To unsubscribe, e-mail:   <mailto:commons-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>


Mime
View raw message