KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > hivemind > util > StringUtils


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.hivemind.util;
16
17 import java.util.ArrayList JavaDoc;
18 import java.util.List JavaDoc;
19
20 /**
21  * A subset of the utilities available in commons-lang StringUtils. It's all
22  * about reducing dependencies, baby!
23  *
24  * @author Howard Lewis Ship
25  */

26 public class StringUtils
27 {
28
29     /**
30      * Splits an input string into a an array of strings, seperating
31      * at commas.
32      *
33      * @param input the string to split, possibly null or empty
34      * @return an array of the strings split at commas
35      */

36     public static String JavaDoc[] split(String JavaDoc input)
37     {
38         if (input == null)
39             return new String JavaDoc[0];
40
41         List JavaDoc strings = new ArrayList JavaDoc();
42
43         int startx = 0;
44         int cursor = 0;
45         int length = input.length();
46
47         while (cursor < length)
48         {
49             if (input.charAt(cursor) == ',')
50             {
51                 String JavaDoc item = input.substring(startx, cursor);
52                 strings.add(item);
53                 startx = cursor + 1;
54             }
55
56             cursor++;
57         }
58
59         if (startx < length)
60             strings.add(input.substring(startx));
61
62         return (String JavaDoc[]) strings.toArray(new String JavaDoc[strings.size()]);
63     }
64
65     /**
66      * Converts a string such that the first character is upper case.
67      *
68      * @param input the input string (possibly empty)
69      * @return the string with the first character converted from lowercase to upper case (may
70      * return the string unchanged if already capitalized)
71      */

72
73     public static String JavaDoc capitalize(String JavaDoc input)
74     {
75         if (input.length() == 0)
76             return input;
77
78         char ch = input.charAt(0);
79
80         if (Character.isUpperCase(ch))
81             return input;
82
83         return String.valueOf(Character.toUpperCase(ch)) + input.substring(1);
84     }
85
86     public static String JavaDoc join(String JavaDoc[] input, char separator)
87     {
88         if (input == null || input.length == 0)
89             return null;
90
91         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
92
93         for (int i = 0; i < input.length; i++)
94         {
95             if (i > 0)
96                 buffer.append(separator);
97
98             buffer.append(input[i]);
99         }
100
101         return buffer.toString();
102     }
103
104     /**
105      * Replaces all occurrences of <code>pattern</code> in
106      * <code>string</code> with <code>replacement</code>
107      */

108     public static String JavaDoc replace(String JavaDoc string, String JavaDoc pattern, String JavaDoc replacement)
109     {
110         StringBuffer JavaDoc sbuf = new StringBuffer JavaDoc();
111         int index = string.indexOf(pattern);
112         int pos = 0;
113         int patternLength = pattern.length();
114         for(; index >= 0; index = string.indexOf(pattern, pos))
115         {
116             sbuf.append(string.substring(pos, index));
117             sbuf.append(replacement);
118             pos = index + patternLength;
119         }
120         sbuf.append(string.substring(pos));
121         
122         return sbuf.toString();
123     }
124     
125 }
126
Popular Tags