KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > modfact > corba > helper > StringHelper


1 /**
2  * copyright 2002 2003 Laboratoire d'Informatique Paris 6 (LIP6)
3  *
4  * This file is part of ModFact.
5  *
6  * ModFact is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * at your option) any later version.
10  *
11  * ModFact 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 for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with ModFact; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  */

20 package org.objectweb.modfact.corba.helper;
21
22 import java.util.StringTokenizer JavaDoc;
23
24 /**
25  * This class provides several functions to manipulate Strings.
26  */

27 public class StringHelper {
28
29     /**
30      * Replace the first occurence of the given string with the given replacement.
31      * @param original The original String to modify.
32      * @param to_replace The substring of the string to search and replace.
33      * @param new_value The replacement String.
34      * @return The modified String.
35      */

36     public static String JavaDoc replaceFirst(String JavaDoc original, String JavaDoc to_replace, String JavaDoc new_value) {
37         int index = original.indexOf(to_replace);
38         if (index != -1) {
39             return original.substring(0, index) + new_value + original.substring(index+to_replace.length());
40         } else {
41             return original;
42         }
43     }
44
45     /**
46      * Replace all the occurences of the given string with the given replacement.
47      * @param original The original String to modify.
48      * @param to_replace The substring of the string to search and replace.
49      * @param new_value The replacement String.
50      * @return The modified String.
51      */

52     public static String JavaDoc replaceAll(String JavaDoc original, String JavaDoc to_replace, String JavaDoc new_value) {
53         StringBuffer JavaDoc newString = new StringBuffer JavaDoc();
54         int indexBegin = 0;
55         int index = original.indexOf(to_replace, indexBegin);
56         while (index != -1) {
57             newString.append(original.substring(indexBegin, index));
58             newString.append(new_value);
59             indexBegin = index + to_replace.length();
60             index = original.indexOf(to_replace, indexBegin);
61         }
62         newString.append(original.substring(indexBegin));
63         return newString.toString();
64     }
65     
66     /**
67      * Parse a String and test if it corresponds to the value. This value can be multiple and separate by '|', for example,
68      * matchesValues(to_parse, new String("value1|value2|value3")).
69      * @param to_parse The substring of the string to search and replace.
70      * @param value The replacement String.
71      * @return TRUE if the parsed string corresponds to one of the values.
72      */

73     public static boolean matchesValues(String JavaDoc to_parse, String JavaDoc value) {
74         StringTokenizer JavaDoc token = new StringTokenizer JavaDoc(value, "|");
75         while (token.hasMoreTokens()) {
76             if (to_parse.equals(token.nextToken()))
77                 return true;
78         }
79         return false;
80     }
81     
82     /**
83       * Parse a String and test if it ends with the value. This value can be multiple and separate by '|', for example,
84       * matchesValues(to_parse, new String("end1|end2|end3")).
85       * @param to_parse The substring of the string to search and replace.
86       * @param value The replacement String.
87       * @return TRUE if the parsed string ends with on of the values.
88       */

89     public static boolean endsWithValues(String JavaDoc to_parse, String JavaDoc value) {
90         StringTokenizer JavaDoc token = new StringTokenizer JavaDoc(value, "|");
91         while (token.hasMoreTokens()) {
92             if (to_parse.endsWith(token.nextToken()))
93                 return true;
94         }
95         return false;
96     }
97
98 }
99
Popular Tags