01: /*
02: * Copyright (c) 2002-2003 by OpenSymphony
03: * All rights reserved.
04: */
05: package com.opensymphony.oscache.util;
06:
07: import java.util.ArrayList;
08: import java.util.List;
09:
10: /**
11: * Provides common utility methods for handling strings.
12: *
13: * @author <a href="mailto:chris@swebtec.com">Chris Miller</a>
14: */
15: public class StringUtil {
16:
17: private StringUtil() {
18: }
19:
20: /**
21: * Splits a string into substrings based on the supplied delimiter
22: * character. Each extracted substring will be trimmed of leading
23: * and trailing whitespace.
24: *
25: * @param str The string to split
26: * @param delimiter The character that delimits the string
27: * @return A string array containing the resultant substrings
28: */
29: public static final List split(String str, char delimiter) {
30: // return no groups if we have an empty string
31: if ((str == null) || "".equals(str)) {
32: return new ArrayList();
33: }
34:
35: ArrayList parts = new ArrayList();
36: int currentIndex;
37: int previousIndex = 0;
38:
39: while ((currentIndex = str.indexOf(delimiter, previousIndex)) > 0) {
40: String part = str.substring(previousIndex, currentIndex)
41: .trim();
42: parts.add(part);
43: previousIndex = currentIndex + 1;
44: }
45:
46: parts.add(str.substring(previousIndex, str.length()).trim());
47:
48: return parts;
49: }
50:
51: /**
52: * @param s the string to be checked
53: * @return true if the string parameter contains at least one element
54: */
55: public static final boolean hasLength(String s) {
56: return (s != null) && (s.length() > 0);
57: }
58:
59: /**
60: * @param s the string to be checked
61: * @return true if the string parameter is null or doesn't contain any element
62: * @since 2.4
63: */
64: public static final boolean isEmpty(String s) {
65: return (s == null) || (s.length() == 0);
66: }
67:
68: }
|