001: /**
002: * com.mckoi.util.StringUtil 17 Dec 1999
003: *
004: * Mckoi SQL Database ( http://www.mckoi.com/database )
005: * Copyright (C) 2000, 2001, 2002 Diehl and Associates, Inc.
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * Version 2 as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License Version 2 for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * Version 2 along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: * Change Log:
021: *
022: *
023: */package com.mckoi.util;
024:
025: import java.util.ArrayList;
026: import java.util.List;
027: import java.util.Iterator;
028: import java.io.*;
029:
030: /**
031: * Various String utilities.
032: *
033: * @author Tobias Downer
034: */
035:
036: public class StringUtil {
037:
038: /**
039: * Finds the index of the given string in the source string.
040: * <p>
041: * @return -1 if the 'find' string could not be found.
042: */
043: public static int find(String source, String find) {
044: return source.indexOf(find);
045:
046: // int find_index = 0;
047: // int len = source.length();
048: // int find_len = find.length();
049: // int i = 0;
050: // for (; i < len; ++i) {
051: // if (find_index == find_len) {
052: // return i - find_len;
053: // }
054: // if (find.indexOf(source.charAt(i), find_index) == find_index) {
055: // ++find_index;
056: // }
057: // else {
058: // find_index = 0;
059: // }
060: // }
061: // if (find_index == find_len) {
062: // return i - find_len;
063: // }
064: // else {
065: // return -1;
066: // }
067: }
068:
069: /**
070: * Performs an 'explode' operation on the given source string. This
071: * algorithm finds all instances of the deliminator string, and returns an
072: * array of sub-strings of between the deliminator. For example,
073: * <code>
074: * explode("10:30:40:55", ":") = ({"10", "30", "40", "55"})
075: * </code>
076: */
077: public static List explode(String source, String deliminator) {
078: ArrayList list = new ArrayList();
079: int i = find(source, deliminator);
080: while (i != -1) {
081: list.add(source.substring(0, i));
082: source = source.substring(i + deliminator.length());
083: i = find(source, deliminator);
084: }
085: list.add(source);
086: return list;
087: }
088:
089: /**
090: * This is the inverse of 'explode'. It forms a string by concatinating
091: * each string in the list and seperating each with a deliminator string.
092: * For example,
093: * <code>
094: * implode(({"1", "150", "500"}), ",") = "1,150,500"
095: * </code>
096: */
097: public static String implode(List list, String deliminator) {
098: StringBuffer str = new StringBuffer();
099: Iterator iter = list.iterator();
100: boolean has_next = iter.hasNext();
101: while (has_next) {
102: str.append(iter.next().toString());
103: has_next = iter.hasNext();
104: if (has_next) {
105: str.append(deliminator);
106: }
107: }
108: return new String(str);
109: }
110:
111: /**
112: * Searches for various instances of the 'search' string and replaces them
113: * with the 'replace' string.
114: */
115: public static String searchAndReplace(String source, String search,
116: String replace) {
117: return implode(explode(source, search), replace);
118: }
119:
120: }
|