1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| package org.apache.hivemind.util; |
16 |
| |
17 |
| import java.util.ArrayList; |
18 |
| import java.util.List; |
19 |
| |
20 |
| |
21 |
| |
22 |
| |
23 |
| |
24 |
| |
25 |
| |
26 |
| public class StringUtils |
27 |
| { |
28 |
| |
29 |
| |
30 |
| |
31 |
| |
32 |
| |
33 |
| |
34 |
| |
35 |
| |
36 |
168
| public static String[] split(String input)
|
37 |
| { |
38 |
168
| if (input == null)
|
39 |
122
| return new String[0];
|
40 |
| |
41 |
46
| List strings = new ArrayList();
|
42 |
| |
43 |
46
| int startx = 0;
|
44 |
46
| int cursor = 0;
|
45 |
46
| int length = input.length();
|
46 |
| |
47 |
46
| while (cursor < length)
|
48 |
| { |
49 |
552
| if (input.charAt(cursor) == ',')
|
50 |
| { |
51 |
12
| String item = input.substring(startx, cursor);
|
52 |
12
| strings.add(item);
|
53 |
12
| startx = cursor + 1;
|
54 |
| } |
55 |
| |
56 |
552
| cursor++;
|
57 |
| } |
58 |
| |
59 |
46
| if (startx < length)
|
60 |
45
| strings.add(input.substring(startx));
|
61 |
| |
62 |
46
| return (String[]) strings.toArray(new String[strings.size()]);
|
63 |
| } |
64 |
| |
65 |
| |
66 |
| |
67 |
| |
68 |
| |
69 |
| |
70 |
| |
71 |
| |
72 |
| |
73 |
7
| public static String capitalize(String input)
|
74 |
| { |
75 |
7
| if (input.length() == 0)
|
76 |
1
| return input;
|
77 |
| |
78 |
5
| char ch = input.charAt(0);
|
79 |
| |
80 |
5
| if (Character.isUpperCase(ch))
|
81 |
1
| return input;
|
82 |
| |
83 |
4
| return String.valueOf(Character.toUpperCase(ch)) + input.substring(1);
|
84 |
| } |
85 |
| |
86 |
7
| public static String join(String[] input, char separator)
|
87 |
| { |
88 |
7
| if (input == null || input.length == 0)
|
89 |
2
| return null;
|
90 |
| |
91 |
5
| StringBuffer buffer = new StringBuffer();
|
92 |
| |
93 |
5
| for (int i = 0; i < input.length; i++)
|
94 |
| { |
95 |
9
| if (i > 0)
|
96 |
4
| buffer.append(separator);
|
97 |
| |
98 |
9
| buffer.append(input[i]);
|
99 |
| } |
100 |
| |
101 |
5
| return buffer.toString();
|
102 |
| } |
103 |
| |
104 |
| |
105 |
| |
106 |
| |
107 |
| |
108 |
0
| public static String replace(String string, String pattern, String replacement)
|
109 |
| { |
110 |
0
| StringBuffer sbuf = new StringBuffer();
|
111 |
0
| int index = string.indexOf(pattern);
|
112 |
0
| int pos = 0;
|
113 |
0
| int patternLength = pattern.length();
|
114 |
0
| for(; index >= 0; index = string.indexOf(pattern, pos))
|
115 |
| { |
116 |
0
| sbuf.append(string.substring(pos, index));
|
117 |
0
| sbuf.append(replacement);
|
118 |
0
| pos = index + patternLength;
|
119 |
| } |
120 |
0
| sbuf.append(string.substring(pos));
|
121 |
| |
122 |
0
| return sbuf.toString();
|
123 |
| } |
124 |
| |
125 |
| } |