KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mckoi > util > StringUtil


1 /**
2  * com.mckoi.util.StringUtil 17 Dec 1999
3  *
4  * Mckoi SQL Database ( http://www.mckoi.com/database )
5  * Copyright (C) 2000, 2001, 2002 Diehl and Associates, Inc.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * Version 2 as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License Version 2 for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * Version 2 along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  *
20  * Change Log:
21  *
22  *
23  */

24
25 package com.mckoi.util;
26
27 import java.util.ArrayList JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.io.*;
31
32 /**
33  * Various String utilities.
34  *
35  * @author Tobias Downer
36  */

37
38 public class StringUtil {
39
40   /**
41    * Finds the index of the given string in the source string.
42    * <p>
43    * @return -1 if the 'find' string could not be found.
44    */

45   public static int find(String JavaDoc source, String JavaDoc find) {
46     return source.indexOf(find);
47
48 // int find_index = 0;
49
// int len = source.length();
50
// int find_len = find.length();
51
// int i = 0;
52
// for (; i < len; ++i) {
53
// if (find_index == find_len) {
54
// return i - find_len;
55
// }
56
// if (find.indexOf(source.charAt(i), find_index) == find_index) {
57
// ++find_index;
58
// }
59
// else {
60
// find_index = 0;
61
// }
62
// }
63
// if (find_index == find_len) {
64
// return i - find_len;
65
// }
66
// else {
67
// return -1;
68
// }
69
}
70
71   /**
72    * Performs an 'explode' operation on the given source string. This
73    * algorithm finds all instances of the deliminator string, and returns an
74    * array of sub-strings of between the deliminator. For example,
75    * <code>
76    * explode("10:30:40:55", ":") = ({"10", "30", "40", "55"})
77    * </code>
78    */

79   public static List JavaDoc explode(String JavaDoc source, String JavaDoc deliminator) {
80     ArrayList JavaDoc list = new ArrayList JavaDoc();
81     int i = find(source, deliminator);
82     while (i != -1) {
83       list.add(source.substring(0, i));
84       source = source.substring(i + deliminator.length());
85       i = find(source, deliminator);
86     }
87     list.add(source);
88     return list;
89   }
90
91   /**
92    * This is the inverse of 'explode'. It forms a string by concatinating
93    * each string in the list and seperating each with a deliminator string.
94    * For example,
95    * <code>
96    * implode(({"1", "150", "500"}), ",") = "1,150,500"
97    * </code>
98    */

99   public static String JavaDoc implode(List JavaDoc list, String JavaDoc deliminator) {
100     StringBuffer JavaDoc str = new StringBuffer JavaDoc();
101     Iterator JavaDoc iter = list.iterator();
102     boolean has_next = iter.hasNext();
103     while (has_next) {
104       str.append(iter.next().toString());
105       has_next = iter.hasNext();
106       if (has_next) {
107         str.append(deliminator);
108       }
109     }
110     return new String JavaDoc(str);
111   }
112
113   /**
114    * Searches for various instances of the 'search' string and replaces them
115    * with the 'replace' string.
116    */

117   public static String JavaDoc searchAndReplace(
118                                 String JavaDoc source, String JavaDoc search, String JavaDoc replace) {
119     return implode(explode(source, search), replace);
120   }
121
122 }
123
Popular Tags