001: // Copyright 2004, 2005 The Apache Software Foundation
002: //
003: // Licensed under the Apache License, Version 2.0 (the "License");
004: // you may not use this file except in compliance with the License.
005: // You may obtain a copy of the License at
006: //
007: // http://www.apache.org/licenses/LICENSE-2.0
008: //
009: // Unless required by applicable law or agreed to in writing, software
010: // distributed under the License is distributed on an "AS IS" BASIS,
011: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: // See the License for the specific language governing permissions and
013: // limitations under the License.
014:
015: package org.apache.hivemind.util;
016:
017: import java.util.ArrayList;
018: import java.util.List;
019:
020: /**
021: * A subset of the utilities available in commons-lang StringUtils. It's all
022: * about reducing dependencies, baby!
023: *
024: * @author Howard Lewis Ship
025: */
026: public class StringUtils {
027:
028: /**
029: * Splits an input string into a an array of strings, seperating
030: * at commas.
031: *
032: * @param input the string to split, possibly null or empty
033: * @return an array of the strings split at commas
034: */
035: public static String[] split(String input) {
036: if (input == null)
037: return new String[0];
038:
039: List strings = new ArrayList();
040:
041: int startx = 0;
042: int cursor = 0;
043: int length = input.length();
044:
045: while (cursor < length) {
046: if (input.charAt(cursor) == ',') {
047: String item = input.substring(startx, cursor);
048: strings.add(item);
049: startx = cursor + 1;
050: }
051:
052: cursor++;
053: }
054:
055: if (startx < length)
056: strings.add(input.substring(startx));
057:
058: return (String[]) strings.toArray(new String[strings.size()]);
059: }
060:
061: /**
062: * Converts a string such that the first character is upper case.
063: *
064: * @param input the input string (possibly empty)
065: * @return the string with the first character converted from lowercase to upper case (may
066: * return the string unchanged if already capitalized)
067: */
068:
069: public static String capitalize(String input) {
070: if (input.length() == 0)
071: return input;
072:
073: char ch = input.charAt(0);
074:
075: if (Character.isUpperCase(ch))
076: return input;
077:
078: return String.valueOf(Character.toUpperCase(ch))
079: + input.substring(1);
080: }
081:
082: public static String join(String[] input, char separator) {
083: if (input == null || input.length == 0)
084: return null;
085:
086: StringBuffer buffer = new StringBuffer();
087:
088: for (int i = 0; i < input.length; i++) {
089: if (i > 0)
090: buffer.append(separator);
091:
092: buffer.append(input[i]);
093: }
094:
095: return buffer.toString();
096: }
097:
098: /**
099: * Replaces all occurrences of <code>pattern</code> in
100: * <code>string</code> with <code>replacement</code>
101: */
102: public static String replace(String string, String pattern,
103: String replacement) {
104: StringBuffer sbuf = new StringBuffer();
105: int index = string.indexOf(pattern);
106: int pos = 0;
107: int patternLength = pattern.length();
108: for (; index >= 0; index = string.indexOf(pattern, pos)) {
109: sbuf.append(string.substring(pos, index));
110: sbuf.append(replacement);
111: pos = index + patternLength;
112: }
113: sbuf.append(string.substring(pos));
114:
115: return sbuf.toString();
116: }
117:
118: }
|