KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jmanage > util > StringUtils


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

16 package org.jmanage.util;
17
18 import java.util.*;
19 import java.lang.reflect.Array JavaDoc;
20 import java.net.URL JavaDoc;
21
22 /**
23  *
24  * date: Feb 4, 2005
25  * @author Rakesh Kalra
26  */

27 public class StringUtils {
28
29     /**
30      * Converts a csv to String[]
31      *
32      * @param csv
33      * @return
34      */

35     public static String JavaDoc[] csvToStringArray(String JavaDoc csv){
36
37         if(csv == null){
38             return null;
39         }
40         StringTokenizer tokenizer = new StringTokenizer(csv, ",");
41         String JavaDoc[] array = new String JavaDoc[tokenizer.countTokens()];
42         int index = 0;
43         while(tokenizer.hasMoreTokens()){
44             array[index ++] = tokenizer.nextToken().trim();
45         }
46         return array;
47     }
48
49     /**
50      * Converts a String[] to comma delimited String
51      */

52     public static String JavaDoc stringArrayToCSV(String JavaDoc[] array){
53         StringBuffer JavaDoc buff = new StringBuffer JavaDoc();
54         for(int i=0; array != null && i<array.length; i++){
55             buff.append(array[i]);
56             if(i < array.length){
57                 buff.append(",");
58             }
59         }
60         return buff.toString();
61     }
62
63     public static String JavaDoc[] listToStringArray(List list){
64         String JavaDoc[] output = new String JavaDoc[list.size()];
65         int i=0;
66         for(Iterator it=list.iterator(); it.hasNext(); i++){
67             output[i] = it.next().toString();
68         }
69         return output;
70     }
71
72     public static List csvToList(String JavaDoc csv) {
73         if(csv == null){
74             return null;
75         }
76         StringTokenizer tokenizer = new StringTokenizer(csv, ",");
77         List list = new ArrayList(tokenizer.countTokens());
78         while(tokenizer.hasMoreTokens()){
79             list.add(tokenizer.nextToken().trim());
80         }
81         return list;
82     }
83
84     public static String JavaDoc toString(URL JavaDoc[] urls){
85         StringBuffer JavaDoc buff = new StringBuffer JavaDoc();
86         for(int i=0; i<urls.length; i++){
87             buff.append(urls[i].toString());
88             buff.append(";");
89         }
90         return buff.toString();
91     }
92
93     public static String JavaDoc padRight(String JavaDoc str, int totalLength) {
94         int strLen = str.length();
95         StringBuffer JavaDoc buff = new StringBuffer JavaDoc(str);
96         for(int i=0;i<totalLength - strLen; i++){
97             buff.append(' ');
98         }
99         return buff.toString();
100     }
101
102     public static String JavaDoc getCharSeries(char ch, int length) {
103         char[] series = new char[length];
104         Arrays.fill(series, ch);
105         return new String JavaDoc(series);
106     }
107
108     public static String JavaDoc htmlEscape(String JavaDoc str){
109         StringBuffer JavaDoc buff = new StringBuffer JavaDoc(str.length());
110         for(int i=0; i<str.length(); i++){
111             final char ch = str.charAt(i);
112             if(ch == '"'){
113                 buff.append("&quot;");
114             }else if(ch == '<'){
115                 buff.append("&lt;");
116             }else if(ch == '>'){
117                 buff.append("&gt;");
118             }else{
119                 buff.append(ch);
120             }
121         }
122         return buff.toString();
123     }
124 }
125
Popular Tags