KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.objectweb.modfact.jmi.helper;
2
3 import java.util.StringTokenizer JavaDoc;
4
5 /**
6  * This class provides several functions to manipulate Strings.
7  */

8 public class StringHelper {
9
10     /**
11      * Replace the first occurence of the given string with the given replacement.
12      * @param original The original String to modify.
13      * @param to_replace The substring of the string to search and replace.
14      * @param new_value The replacement String.
15      * @return The modified String.
16      */

17     public static String JavaDoc replaceFirst(String JavaDoc original, String JavaDoc to_replace, String JavaDoc new_value) {
18         int index = original.indexOf(to_replace);
19         if (index != -1) {
20             return original.substring(0, index) + new_value + original.substring(index+to_replace.length());
21         } else {
22             return original;
23         }
24     }
25
26     /**
27      * Replace all the occurences of the given string with the given replacement.
28      * @param original The original String to modify.
29      * @param to_replace The substring of the string to search and replace.
30      * @param new_value The replacement String.
31      * @return The modified String.
32      */

33     public static String JavaDoc replaceAll(String JavaDoc original, String JavaDoc to_replace, String JavaDoc new_value) {
34         StringBuffer JavaDoc newString = new StringBuffer JavaDoc();
35         int indexBegin = 0;
36         int index = original.indexOf(to_replace, indexBegin);
37         while (index != -1) {
38             newString.append(original.substring(indexBegin, index));
39             newString.append(new_value);
40             indexBegin = index + to_replace.length();
41             index = original.indexOf(to_replace, indexBegin);
42         }
43         newString.append(original.substring(indexBegin));
44         return newString.toString();
45     }
46     
47     /**
48      * Parse a String and test if it corresponds to the value. This value can be multiple and separate by '|', for example,
49      * matchesValues(to_parse, new String("value1|value2|value3")).
50      * @param to_parse The substring of the string to search and replace.
51      * @param value The replacement String.
52      * @return TRUE if the parsed string corresponds to one of the values.
53      */

54     public static boolean matchesValues(String JavaDoc to_parse, String JavaDoc value) {
55         StringTokenizer JavaDoc token = new StringTokenizer JavaDoc(value, "|");
56         while (token.hasMoreTokens()) {
57             if (to_parse.equals(token.nextToken()))
58                 return true;
59         }
60         return false;
61     }
62     
63     /**
64       * Parse a String and test if it ends with the value. This value can be multiple and separate by '|', for example,
65       * matchesValues(to_parse, new String("end1|end2|end3")).
66       * @param to_parse The substring of the string to search and replace.
67       * @param value The replacement String.
68       * @return TRUE if the parsed string ends with on of the values.
69       */

70     public static boolean endsWithValues(String JavaDoc to_parse, String JavaDoc value) {
71         StringTokenizer JavaDoc token = new StringTokenizer JavaDoc(value, "|");
72         while (token.hasMoreTokens()) {
73             if (to_parse.endsWith(token.nextToken()))
74                 return true;
75         }
76         return false;
77     }
78
79     /**
80      * Remove all the occurences which match the pattern.
81      * @param original The original String to modify.
82      * @param pattern The pattern string to search and remove.
83      * This pattern could be composed by character, range (a-z),
84      * and start with "[" or "[^" (negation) and stop with "]".
85      * @return The modified String.
86      */

87     public static String JavaDoc removeAll(String JavaDoc original, String JavaDoc pattern) {
88         // Builds pattern to search.
89
boolean negation = false;
90         String JavaDoc toSearch = pattern;
91         if (toSearch.startsWith("[") && toSearch.endsWith("]")) {
92             toSearch = toSearch.substring(1, toSearch.length()-1);
93             if (toSearch.startsWith("^")) {
94                 toSearch = toSearch.substring(1);
95                 negation = true;
96             }
97         }
98         // Parses pattern.
99
java.util.List JavaDoc characters = new java.util.ArrayList JavaDoc();
100         for (int i=0 ; i<toSearch.length() ; i++) {
101             if (i>1 && toSearch.charAt(i-1)=='-') {
102                 for (char c=toSearch.charAt(i-2) ; c<=toSearch.charAt(i) ; c++) {
103                     characters.add(new Character JavaDoc(c));
104                 }
105             } else if (toSearch.charAt(i) != '-')
106                 characters.add(new Character JavaDoc(toSearch.charAt(i)));
107         }
108         // Parses original string.
109
StringBuffer JavaDoc newString = new StringBuffer JavaDoc();
110         for (int i=0 ; i<original.length() ; i++) {
111             char c = original.charAt(i);
112             if (!negation && !characters.contains(new Character JavaDoc(c)))
113                 newString.append(c);
114             else if (negation && characters.contains(new Character JavaDoc(c)))
115                 newString.append(c);
116         }
117         return newString.toString();
118     }
119
120 }
121
Popular Tags