KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > firstpartners > nounit > utility > TextUtil


1 package net.firstpartners.nounit.utility;
2
3 /**
4  * Title: NoUnit - Identify Classes that are not being unit Tested
5  *
6  * Copyright (C) 2001 Paul Browne , FirstPartners.net
7  *
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * @author Paul Browne
24  * @version 0.7
25  */

26
27 /**
28  * Performs common , but tricky tasks.
29  * e.g. Stripping of spaces from Files
30  */

31 public class TextUtil {
32
33   /**
34    * Replace the first occurrence of <code>oldSubstring</code> in
35    * <code>string</code>, if there is one, with <code>newSubstring</code>.
36    *
37    * @param string - Replace a substring of this String
38    * @param oldSubstring - The substring of <code>string</code> to be replaced
39    * @param newSubstring - The string to put into <code> string</code>
40    * @return A new String which is a copy of <code>string</code> with the first
41    * occurrence of <code>oldSubstring</code> in <code>string</code>, if
42    * there is one, with <code>newSubstring</code>.
43    * Returns null if <code>string</code> is null.
44    * Returns <code>string</code> if either substring is null or
45    * <code>oldSubstring</code> is empty
46    */

47   public static String JavaDoc replace(String JavaDoc string,
48                                String JavaDoc oldSubstring,
49                                String JavaDoc newSubstring) {
50     String JavaDoc result = string;
51
52     if ((string != null) && (string.length() > 0)
53         && (oldSubstring != null) && (oldSubstring.length() > 0)
54         && (newSubstring != null))
55     {
56       int pos = string.indexOf(oldSubstring);
57       result = string.substring(0, pos)
58                + newSubstring
59                + string.substring(pos + oldSubstring.length());
60     }
61
62     return result;
63   }
64
65
66   /**
67    * Replaces all occurrences of <code>oldSubstring</code> in
68    * <code>string</code>, if there are any, with <code>newSubstring</code>.
69    *
70    * @param string - Replace substrings of this String
71    * @param oldSubstring - The substring of <code>string</code> to be replaced
72    * @param newSubstring - The string to put into <code> string</code>
73    * @return A new String which is a copy of <code>string</code> with all
74    * occurrences of <code>oldSubstring</code> in <code>string</code>,
75    * if there are any, with <code>newSubstring</code>.
76    * Returns null if <code>string</code> is null.
77    * Returns <code>string</code> if either substring is null or
78    * <code>oldSubstring</code> is empty
79    */

80   public static String JavaDoc replaceAll( String JavaDoc string,
81                                    String JavaDoc oldSubstring,
82                                    String JavaDoc newSubstring) {
83     //Local Variables
84
String JavaDoc result = string;
85
86     
87     
88     if ( (result != null)
89          && (result.length() > 0)
90          && (result.indexOf(oldSubstring)>-1)
91          && (oldSubstring.length() > 0)
92          && (!oldSubstring.equals(newSubstring))) {
93              
94       while (result.indexOf(oldSubstring) > -1) {
95         result = replace(result, oldSubstring, newSubstring);
96       }
97     }
98
99     return result;
100   }
101
102
103   /**
104    * Finds (start and end) markers in piece of text
105    * extracts text (note including markers) in between
106    * @param fullText to search in
107    * @param startIndex ignore text before this point
108    * @param startMarker start marker for the piece of text to extract
109    * @param endMarker end marker
110    * @return foundText , empty String if nothing found
111    */

112    public static String JavaDoc find(String JavaDoc fullText,
113                               int startIndex,
114                               String JavaDoc startMarker,
115                               String JavaDoc endMarker) {
116
117       //Local Variables
118
int startPlace=0;
119       int endPlace=0;
120       String JavaDoc foundText="";
121
122       //Find the first instance of text
123
startPlace = fullText.indexOf(startMarker,startIndex)
124                         +startMarker.length();
125
126       //Find the first instance of end marker after this
127
if (startPlace > startIndex) {
128         startIndex = startPlace;
129       }
130       endPlace = fullText.indexOf(endMarker,startIndex);
131
132       //Copy and return
133
try {
134         if ((startPlace>-1) || (endPlace>-1)) {
135           foundText=fullText.substring(startPlace,endPlace);
136         }
137       } catch (java.lang.StringIndexOutOfBoundsException JavaDoc sioobe) {
138         // do nothing - will return default of empty string
139
}
140
141       //Ensure that there are no dodgy strings..
142
if (startPlace<startIndex) {
143         foundText="";
144       }
145
146
147     return foundText;
148   }
149  
150
151      /**
152    * Remove all instances of input string from Output
153    * @param inputString
154    * @param removeString
155    * @return updateString
156    */

157   public static String JavaDoc removeAll(String JavaDoc inputString,
158                                   String JavaDoc removeString) {
159
160     //Internal Variables
161
StringBuffer JavaDoc updateString = new StringBuffer JavaDoc();
162     String JavaDoc tmpString;
163
164
165     for(int a=0; a<inputString.length(); a++ ) {
166
167       tmpString = inputString.substring(a,a+1);
168       if (!tmpString.equals(removeString)) {
169         updateString.append(tmpString);
170       }
171
172     }
173
174     return updateString.toString();
175
176   }
177
178   /**
179    * Strip out any trailing characters
180    * @param inString to be operated on
181    * @param toRemove string to remove at end if found
182    * @return inString with end removed
183    */

184   public static String JavaDoc removeTrailing (String JavaDoc inString,
185                                             String JavaDoc toRemove) {
186     while (inString.endsWith(toRemove)) {
187         inString=inString.substring(0,inString.length()-toRemove.length());
188     }
189
190     return inString;
191
192   }
193
194 }
Popular Tags