KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sape > carbon > core > util > string > StringUtil


1 /*
2  * The contents of this file are subject to the Sapient Public License
3  * Version 1.0 (the "License"); you may not use this file except in compliance
4  * with the License. You may obtain a copy of the License at
5  * http://carbon.sf.net/License.html.
6  *
7  * Software distributed under the License is distributed on an "AS IS" basis,
8  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
9  * the specific language governing rights and limitations under the License.
10  *
11  * The Original Code is The Carbon Component Framework.
12  *
13  * The Initial Developer of the Original Code is Sapient Corporation
14  *
15  * Copyright (C) 2003 Sapient Corporation. All Rights Reserved.
16  */

17
18 package org.sape.carbon.core.util.string;
19
20 import java.util.ArrayList JavaDoc;
21 import java.util.HashSet JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.Set JavaDoc;
24 import java.util.StringTokenizer JavaDoc;
25
26 /**
27  * <p>Utilities for strings.</p>
28  *
29  *
30  * Copyright 2002 Sapient
31  * @since carbon 1.0
32  * @author Greg Hinkle, May 2002
33  * @version $Revision: 1.5 $($Author: dvoet $ / $Date: 2003/05/05 21:21:24 $)
34  */

35 public class StringUtil {
36
37     /**
38      * Capitalizes the first character of the given string.
39      * @param s the String to capitalize
40      * @return the capitalized result
41      */

42     public static String JavaDoc capitalize(String JavaDoc s) {
43         if (s.length() == 0) return s;
44         char c = Character.toUpperCase(s.charAt(0));
45         return c + s.substring(1, s.length());
46     }
47
48     /**
49      * Parses a comma-separated list into an array of Strings
50      * Values can contain whitespace, but whitespace at the beginning and
51      * end of each value is trimmed.
52      * @return array of Strings
53      * @param csvList a string of comma seperated values
54      */

55     public static String JavaDoc[] parseCommaDelimitedList(String JavaDoc csvList) {
56         String JavaDoc[] result = parseList(csvList, ",");
57         for (int i = 0; i < result.length; i++) {
58             result[i] = result[i].trim();
59         }
60         return result;
61     }
62
63     /**
64      * Parses a whitepsace-separated list into an array of Strings
65      * @return array of Strings
66      * @param wsvList a string of white space seperated values
67      */

68     public static String JavaDoc[] parseWhitespaceDelimitedList(String JavaDoc wsvList) {
69         return parseList(wsvList, "\t ");
70     }
71
72     /**
73      * Parses a list according to the specified delimiter into an
74      * array of Strings.
75      * @see java.util.StringTokenizer
76      * @param list a string of token seperated values
77      * @param delim the delimiter character(s). Each character in the string is a
78      * single delimeter.
79      * @return an array of strings
80      */

81     public static String JavaDoc[] parseList(String JavaDoc list, String JavaDoc delim) {
82         List JavaDoc result = new ArrayList JavaDoc();
83         StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(list, delim);
84         while (tokenizer.hasMoreTokens()) {
85             result.add(tokenizer.nextToken());
86         }
87         return (String JavaDoc[]) result.toArray(new String JavaDoc[0]);
88     }
89
90
91
92     /**
93      * <p>Removes specified chars from a string.</p>
94      *
95      * @param aString the string that will be examined to remove chars
96      * @param unWantedCharArray the char array containing the chars
97      * that should be removed from a string
98      * @return the string after removing the specified chars
99      */

100     public static String JavaDoc removeCharsFromString(String JavaDoc aString, char[] unWantedCharArray) {
101
102         Character JavaDoc character = null;
103
104         // Store unwanted chars in a hashset
105
Set JavaDoc unWantedCharSet = new HashSet JavaDoc();
106
107         for (int i = 0; i < unWantedCharArray.length; i++) {
108             character = new Character JavaDoc(unWantedCharArray[i]);
109             unWantedCharSet.add(character);
110         }
111
112         // Create result String buffer
113
StringBuffer JavaDoc result = new StringBuffer JavaDoc(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 JavaDoc(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 JavaDoc convertToSet(String JavaDoc string) {
136
137         // Result hashset
138
Set JavaDoc resultSet = new HashSet JavaDoc();
139
140         for (int i = 0; i < string.length(); i++) {
141             resultSet.add(new Character JavaDoc(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 JavaDoc convertToSet(char[] charArray) {
155
156         // Result hashset
157
Set JavaDoc resultSet = new HashSet JavaDoc();
158
159         for (int i = 0; i < charArray.length; i++) {
160             resultSet.add(new Character JavaDoc(charArray[i]));
161         }
162
163         // Return result
164
return resultSet;
165     }
166
167 }
168
169
Popular Tags