KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > oscache > util > StringUtil


1 /*
2  * Copyright (c) 2002-2003 by OpenSymphony
3  * All rights reserved.
4  */

5 package com.opensymphony.oscache.util;
6
7 import java.util.ArrayList JavaDoc;
8 import java.util.List JavaDoc;
9
10 /**
11  * Provides common utility methods for handling strings.
12  *
13  * @author <a HREF="&#109;a&#105;&#108;&#116;&#111;:chris&#64;swebtec.&#99;&#111;&#109;">Chris Miller</a>
14  */

15 public class StringUtil {
16     
17     /**
18      * Splits a string into substrings based on the supplied delimiter
19      * character. Each extracted substring will be trimmed of leading
20      * and trailing whitespace.
21      *
22      * @param str The string to split
23      * @param delimiter The character that delimits the string
24      * @return A string array containing the resultant substrings
25      */

26     public static final List JavaDoc split(String JavaDoc str, char delimiter) {
27         // return no groups if we have an empty string
28
if ((str == null) || "".equals(str)) {
29             return new ArrayList JavaDoc();
30         }
31
32         ArrayList JavaDoc parts = new ArrayList JavaDoc();
33         int currentIndex;
34         int previousIndex = 0;
35
36         while ((currentIndex = str.indexOf(delimiter, previousIndex)) > 0) {
37             String JavaDoc part = str.substring(previousIndex, currentIndex).trim();
38             parts.add(part);
39             previousIndex = currentIndex + 1;
40         }
41
42         parts.add(str.substring(previousIndex, str.length()).trim());
43
44         return parts;
45     }
46     
47     /**
48      * @param s the string to be checked
49      * @return true if the string parameter contains at least one element
50      */

51     public static final boolean hasLength(String JavaDoc s) {
52         return (s != null) && (s.length() > 0);
53     }
54     
55 }
56
Popular Tags