001: /*
002: * The contents of this file are subject to the Sapient Public License
003: * Version 1.0 (the "License"); you may not use this file except in compliance
004: * with the License. You may obtain a copy of the License at
005: * http://carbon.sf.net/License.html.
006: *
007: * Software distributed under the License is distributed on an "AS IS" basis,
008: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
009: * the specific language governing rights and limitations under the License.
010: *
011: * The Original Code is The Carbon Component Framework.
012: *
013: * The Initial Developer of the Original Code is Sapient Corporation
014: *
015: * Copyright (C) 2003 Sapient Corporation. All Rights Reserved.
016: */
017:
018: package org.sape.carbon.core.util.string;
019:
020: import java.util.ArrayList;
021: import java.util.HashSet;
022: import java.util.List;
023: import java.util.Set;
024: import java.util.StringTokenizer;
025:
026: /**
027: * <p>Utilities for strings.</p>
028: *
029: *
030: * Copyright 2002 Sapient
031: * @since carbon 1.0
032: * @author Greg Hinkle, May 2002
033: * @version $Revision: 1.5 $($Author: dvoet $ / $Date: 2003/05/05 21:21:24 $)
034: */
035: public class StringUtil {
036:
037: /**
038: * Capitalizes the first character of the given string.
039: * @param s the String to capitalize
040: * @return the capitalized result
041: */
042: public static String capitalize(String s) {
043: if (s.length() == 0)
044: return s;
045: char c = Character.toUpperCase(s.charAt(0));
046: return c + s.substring(1, s.length());
047: }
048:
049: /**
050: * Parses a comma-separated list into an array of Strings
051: * Values can contain whitespace, but whitespace at the beginning and
052: * end of each value is trimmed.
053: * @return array of Strings
054: * @param csvList a string of comma seperated values
055: */
056: public static String[] parseCommaDelimitedList(String csvList) {
057: String[] result = parseList(csvList, ",");
058: for (int i = 0; i < result.length; i++) {
059: result[i] = result[i].trim();
060: }
061: return result;
062: }
063:
064: /**
065: * Parses a whitepsace-separated list into an array of Strings
066: * @return array of Strings
067: * @param wsvList a string of white space seperated values
068: */
069: public static String[] parseWhitespaceDelimitedList(String wsvList) {
070: return parseList(wsvList, "\t ");
071: }
072:
073: /**
074: * Parses a list according to the specified delimiter into an
075: * array of Strings.
076: * @see java.util.StringTokenizer
077: * @param list a string of token seperated values
078: * @param delim the delimiter character(s). Each character in the string is a
079: * single delimeter.
080: * @return an array of strings
081: */
082: public static String[] parseList(String list, String delim) {
083: List result = new ArrayList();
084: StringTokenizer tokenizer = new StringTokenizer(list, delim);
085: while (tokenizer.hasMoreTokens()) {
086: result.add(tokenizer.nextToken());
087: }
088: return (String[]) result.toArray(new String[0]);
089: }
090:
091: /**
092: * <p>Removes specified chars from a string.</p>
093: *
094: * @param aString the string that will be examined to remove chars
095: * @param unWantedCharArray the char array containing the chars
096: * that should be removed from a string
097: * @return the string after removing the specified chars
098: */
099: public static String removeCharsFromString(String aString,
100: char[] unWantedCharArray) {
101:
102: Character character = null;
103:
104: // Store unwanted chars in a hashset
105: Set unWantedCharSet = new HashSet();
106:
107: for (int i = 0; i < unWantedCharArray.length; i++) {
108: character = new Character(unWantedCharArray[i]);
109: unWantedCharSet.add(character);
110: }
111:
112: // Create result String buffer
113: StringBuffer result = new StringBuffer(aString.length());
114:
115: // For each character in aString, append it to the result string buffer
116: // if it is not in unWantedCharSet
117: for (int i = 0; i < aString.length(); i++) {
118: character = new Character(aString.charAt(i));
119: if (!unWantedCharSet.contains(character)) {
120: result.append(aString.charAt(i));
121: }
122: }
123:
124: // Return result
125: return result.toString();
126: }
127:
128: /**
129: * <p> Converts a string to a Set. Breaks the string to characters and store
130: * each character in a Set.
131: *
132: * @param string the string to convert
133: * @return a <code>Set</code> containing all characters in the text string parameter
134: */
135: public static Set convertToSet(String string) {
136:
137: // Result hashset
138: Set resultSet = new HashSet();
139:
140: for (int i = 0; i < string.length(); i++) {
141: resultSet.add(new Character(string.charAt(i)));
142: }
143:
144: // Return result
145: return resultSet;
146: }
147:
148: /**
149: * <p> Converts a char array to a Set. Puts all characters in the array to a Set.
150: *
151: * @param charArray an array of <CODE>char</CODE>s
152: * @return a <code>set</code> containing all characters in the char array
153: */
154: public static Set convertToSet(char[] charArray) {
155:
156: // Result hashset
157: Set resultSet = new HashSet();
158:
159: for (int i = 0; i < charArray.length; i++) {
160: resultSet.add(new Character(charArray[i]));
161: }
162:
163: // Return result
164: return resultSet;
165: }
166:
167: }
|