View Javadoc

1   /*
2    * Copyright (C) 2004 TiongHiang Lee
3    *
4    * This library is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Lesser General Public
6    * License as published by the Free Software Foundation; either
7    * version 2.1 of the License, or (at your option) any later version.
8    *
9    * This library is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   * Lesser General Public License for more details.
13   *
14   * You should have received a copy of the GNU Lesser General Public
15   * License along with this library; if not,  write to the Free Software
16   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17   *
18   * Email: thlee@onemindsoft.org
19   */
20  
21  package org.onemind.commons.java.util;
22  
23  import java.util.Collection;
24  /***
25   * String utilities method
26   * @author TiongHiang Lee (thlee@onemindsoft.org)
27   * @version $Id: StringUtils.java,v 1.3 2004/09/19 20:07:32 thlee Exp $ $Name:  $
28   */
29  public final class StringUtils
30  {
31  
32      /***
33       * Constructor
34       */
35      private StringUtils()
36      {
37          super();
38      }
39  
40      /***
41       * Concat the collection l to string with delimenter
42       * @param l the collection
43       * @param delimiter the delimiter
44       * @return the result string
45       */
46      public static String concat(Collection l, String delimiter)
47      {
48          return concat(l.toArray(), delimiter);
49      }
50  
51      /***
52       * Concant the object in the array (using objects.toString()), delimited by delimiter
53       * @param objects the objects
54       * @param delimiter the delimiter
55       * @return a string
56       */
57      public static String concat(Object[] objects, String delimiter)
58      {
59          StringBuffer sb = new StringBuffer();
60          for (int i = 0; i < objects.length; i++)
61          {
62              sb.append(objects[i]);
63              if (i < (objects.length - 1))
64              {
65                  sb.append(delimiter);
66              }
67          }
68          return sb.toString();
69      }
70  
71      /***
72       * Return the substring after the first occurrance of pattern
73       * @param str the str
74       * @param pattern the pattern
75       * @return the substring of pattern can be matched in str, or null
76       */
77      public static String substringAfter(String str, String pattern)
78      {
79          int i = str.indexOf(pattern);
80          if (i == -1)
81          {
82              return null;
83          }
84          return str.substring(i + pattern.length());
85      }
86  
87      /***
88       * Return substring of str after the the last occurrance of pattern
89       * @param str the str
90       * @param pattern the pattern
91       * @return the substring if pattern can be matched in the str, or null
92       */
93      public static String substringAfterLast(String str, String pattern)
94      {
95          int i = str.lastIndexOf(pattern);
96          if (i == -1)
97          {
98              return null;
99          }
100         return str.substring(i + pattern.length());
101     }
102 
103     /***
104      * Return substring of str before the last occurrance of str
105      * @param str the str
106      * @param pattern the pattern
107      * @return the substring if pattern can be matched, or null
108      */
109     public static String substringBeforeLast(String str, String pattern)
110     {
111         int i = str.lastIndexOf(pattern);
112         if (i == -1)
113         {
114             return null;
115         }
116         return str.substring(0, i);
117     }
118 
119     /***
120      * Whether the string is empty
121      * @param str whether is null or zero length (after trim)
122      * @return true if null of zero length after trim
123      */
124     public static boolean isNullOrEmpty(String str)
125     {
126         return (str == null || str.trim().length() == 0);
127     }
128 
129     /***
130      * Whether the string is empty
131      * @param str whether is null or zero length (after trim)
132      * @return true if null of zero length after trim
133      */
134     public static boolean isNullOrEmpty(Object strObject)
135     {
136         if (strObject == null)
137         {
138             return true;
139         } else if (strObject instanceof String)
140         {
141             return isNullOrEmpty((String) strObject);
142         } else
143         {
144             return isNullOrEmpty(strObject.toString());
145         }
146     }
147 }