1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package org.dbforms.util;
25
26
27 import java.util.StringTokenizer;
28 import java.util.Vector;
29
30
31
32
33 /***
34 * This utility-class provides convenience methods for parsing strings and
35 * generating certain data structures
36 *
37 * @author Joe Peer
38 */
39 public class StringUtil {
40
41 /***
42 * Method for parsing substring embedded by constant delimeters.
43 * <br>
44 * consider the following string s: ac_update_3_12
45 * <br>
46 * <pre>
47 * getEmbeddedString(s, 0, '_') ==> "ac"
48 * getEmbeddedString(s, 1, '_') ==> "update"
49 * getEmbeddedString(s, 2, '_') ==> "3"
50 * getEmbeddedString(s, 3, '_') ==> "12"
51 * getEmbeddedString(s, 3, '_') ==> will throw a Runtime Exception
52 * </pre>
53 *
54 * @param str the string to parse
55 * @param afterDelims the delimiter occurence where to start to parse
56 * @param delim the delimiter string
57 * @return the substring contained between the <code>afterDelims</code> delimiter occurence
58 * and the next one
59 */
60 public static String getEmbeddedString(String str,
61 int afterDelims,
62 char delim) {
63 int lastIndex = 0;
64
65 for (int i = 0; i < afterDelims; i++) {
66 lastIndex = str.indexOf(delim, lastIndex) + 1;
67 }
68
69 int nextIndex = str.indexOf(delim, lastIndex);
70
71 if (nextIndex == -1) {
72
73 int dotIndex = str.lastIndexOf('.');
74 nextIndex = (dotIndex == -1) ? str.length()
75 : dotIndex;
76 }
77
78 return str.substring(lastIndex, nextIndex);
79 }
80
81 /***
82 * Get the int value of the substring contained between
83 * the <code>afterDelims</code> delimiter occurence and the next one
84 *
85 * @param str the string to parse
86 * @param afterDelims the delimiter occurence where to start to parse
87 * @param delim the delimiter string
88 * @return the int value of the substring contained between
89 * the <code>afterDelims</code> delimiter occurence and the next one
90 */
91 public static int getEmbeddedStringAsInteger(String str,
92 int afterDelims,
93 char delim) {
94 return Integer.parseInt(getEmbeddedString(str, afterDelims, delim));
95 }
96
97 /***
98 * Method for parsing substring embedded by constant delimeters
99 * <br>
100 * Because getEmbeddedString() support "." for image button, this method do
101 * the the same, but ignore dots. It's a patch and must be revised in the
102 * next cleanup... #checkme
103 * <br>
104 * consider the following string s: English-001:param1, param2
105 *
106 * <pre>
107 * getEmbeddedString(s, 0, '_') ==> ""
108 * getEmbeddedString(s, 1, '_') ==> ""
109 * getEmbeddedString(s, 2, '_') ==> ""
110 * getEmbeddedString(s, 3, '_') ==> ""
111 * getEmbeddedString(s, 3, '_') ==> will throw a Runtime Exception
112 * </pre>
113 *
114 * @param str the string to parse
115 * @param afterDelims the delimiter occurence where to start to parse
116 * @param delim the delimiter string
117 *
118 * @return the substring contained between the <code>afterDelims</code> delimiter occurence
119 * and the next one
120 */
121 public static String getEmbeddedStringWithoutDots(String str,
122 int afterDelims,
123 char delim) {
124 int lastIndex = 0;
125
126 for (int i = 0; i < afterDelims; i++) {
127 lastIndex = str.indexOf(delim, lastIndex) + 1;
128 }
129
130 int nextIndex = str.indexOf(delim, lastIndex);
131
132 if (nextIndex == -1) {
133 nextIndex = str.length();
134 }
135
136 return str.substring(lastIndex, nextIndex);
137 }
138
139 /***
140 * Get a Vector object containing all the tokens
141 * related to the input string, splitted using the input delimiter.
142 *
143 * @param str the string to split
144 * @param delimeter the delimiter string
145 *
146 * @return a Vector object containing all the tokens
147 * related to the input string
148 */
149 public static Vector splitString(String str,
150 String delimeter) {
151 Vector result = new Vector();
152 StringTokenizer st = new StringTokenizer(str, delimeter);
153
154 while (st.hasMoreTokens())
155 result.addElement(st.nextToken());
156
157 return result;
158 }
159
160 }