1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.ws.security.util;
18
19 import java.util.ArrayList;
20 import java.util.List;
21
22 public class StringUtil {
23 /***
24 * An empty immutable <code>String</code> array.
25 */
26 public static final String[] EMPTY_STRING_ARRAY = new String[0];
27
28 /***
29 * Tests if this string starts with the specified prefix (Ignoring whitespaces)
30 *
31 * @param prefix
32 * @param string
33 * @return boolean
34 */
35 public static boolean startsWithIgnoreWhitespaces(String prefix, String string) {
36 int index1 = 0;
37 int index2 = 0;
38 int length1 = prefix.length();
39 int length2 = string.length();
40 char ch1 = ' ';
41 char ch2 = ' ';
42 while (index1 < length1 && index2 < length2) {
43 while (index1 < length1 && Character.isWhitespace(ch1 = prefix.charAt(index1))) {
44 index1++;
45 }
46 while (index2 < length2 && Character.isWhitespace(ch2 = string.charAt(index2))) {
47 index2++;
48 }
49 if (index1 == length1 && index2 == length2) {
50 return true;
51 }
52 if (ch1 != ch2) {
53 return false;
54 }
55 index1++;
56 index2++;
57 }
58 if (index1 < length1 && index2 >= length2)
59 return false;
60 return true;
61 }
62
63 /***
64 * <p>Splits the provided text into an array, separator specified.
65 * This is an alternative to using StringTokenizer.</p>
66 * <p/>
67 * <p>The separator is not included in the returned String array.
68 * Adjacent separators are treated as one separator.</p>
69 * <p/>
70 * <p>A <code>null</code> input String returns <code>null</code>.</p>
71 * <p/>
72 * <pre>
73 * StringUtils.split(null, *) = null
74 * StringUtils.split("", *) = []
75 * StringUtils.split("a.b.c", '.') = ["a", "b", "c"]
76 * StringUtils.split("a..b.c", '.') = ["a", "b", "c"]
77 * StringUtils.split("a:b:c", '.') = ["a:b:c"]
78 * StringUtils.split("a\tb\nc", null) = ["a", "b", "c"]
79 * StringUtils.split("a b c", ' ') = ["a", "b", "c"]
80 * </pre>
81 *
82 * @param str the String to parse, may be null
83 * @param separatorChar the character used as the delimiter,
84 * <code>null</code> splits on whitespace
85 * @return an array of parsed Strings, <code>null</code> if null String input
86 */
87 public static String[] split(String str, char separatorChar) {
88 if (str == null) {
89 return null;
90 }
91 int len = str.length();
92 if (len == 0) {
93 return EMPTY_STRING_ARRAY;
94 }
95 List list = new ArrayList();
96 int i = 0, start = 0;
97 boolean match = false;
98 while (i < len) {
99 if (str.charAt(i) == separatorChar) {
100 if (match) {
101 list.add(str.substring(start, i));
102 match = false;
103 }
104 start = ++i;
105 continue;
106 }
107 match = true;
108 i++;
109 }
110 if (match) {
111 list.add(str.substring(start, i));
112 }
113 return (String[]) list.toArray(new String[list.size()]);
114 }
115 }