Find, replace and remove strings
package net.firstpartners.nounit.utility;
/**
* Title: NoUnit - Identify Classes that are not being unit Tested
*
* Copyright (C) 2001 Paul Browne , FirstPartners.net
*
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* @author Paul Browne
* @version 0.6
*/
import java.sql.*;
import java.io.*;
import java.util.*;
import java.text.DateFormat.*;
/**
* Performs common , but tricky tasks.
* e.g. Stripping of spaces from Files
*/
public class TextUtil {
/**
* Replace the first occurrence of <code>oldSubstring</code> in
* <code>string</code>, if there is one, with <code>newSubstring</code>.
*
* @param string - Replace a substring of this String
* @param oldSubstring - The substring of <code>string</code> to be replaced
* @param newSubstring - The string to put into <code> string</code>
* @return A new String which is a copy of <code>string</code> with the first
* occurrence of <code>oldSubstring</code> in <code>string</code>, if
* there is one, with <code>newSubstring</code>.
* Returns null if <code>string</code> is null.
* Returns <code>string</code> if either substring is null or
* <code>oldSubstring</code> is empty
*/
public static String replace(String string,
String oldSubstring,
String newSubstring) {
String result = string;
if ((string != null) && (string.length() > 0)
&& (oldSubstring != null) && (oldSubstring.length() > 0)
&& (newSubstring != null))
{
int pos = string.indexOf(oldSubstring);
result = string.substring(0, pos)
+ newSubstring
+ string.substring(pos + oldSubstring.length());
}
return result;
}
/**
* Replaces all occurrences of <code>oldSubstring</code> in
* <code>string</code>, if there are any, with <code>newSubstring</code>.
*
* @param string - Replace substrings of this String
* @param oldSubstring - The substring of <code>string</code> to be replaced
* @param newSubstring - The string to put into <code> string</code>
* @return A new String which is a copy of <code>string</code> with all
* occurrences of <code>oldSubstring</code> in <code>string</code>,
* if there are any, with <code>newSubstring</code>.
* Returns null if <code>string</code> is null.
* Returns <code>string</code> if either substring is null or
* <code>oldSubstring</code> is empty
*/
public static String replaceAll( String string,
String oldSubstring,
String newSubstring) {
//Local Variables
String result = string;
if ( (result != null)
&& (result.length() > 0)
&& (result.indexOf(oldSubstring)>-1)
&& (oldSubstring.length() > 0)
&& (!oldSubstring.equals(newSubstring))) {
while (result.indexOf(oldSubstring) > -1) {
result = replace(result, oldSubstring, newSubstring);
}
}
return result;
}
/**
* Finds (start and end) markers in piece of text
* extracts text (note including markers) in between
* @param fullText to search in
* @param startIndex ignore text before this point
* @param startMarker start marker for the piece of text to extract
* @param endMarker end marker
* @return foundText , empty String if nothing found
*/
public static String find(String fullText,
int startIndex,
String startMarker,
String endMarker) {
//Local Variables
int startPlace=0;
int endPlace=0;
String foundText="";
//Find the first instance of text
startPlace = fullText.indexOf(startMarker,startIndex)
+startMarker.length();
//Find the first instance of end marker after this
if (startPlace > startIndex) {
startIndex = startPlace;
}
endPlace = fullText.indexOf(endMarker,startIndex);
//Copy and return
try {
if ((startPlace>-1) || (endPlace>-1)) {
foundText=fullText.substring(startPlace,endPlace);
}
} catch (java.lang.StringIndexOutOfBoundsException sioobe) {
// do nothing - will return default of empty string
}
//Ensure that there are no dodgy strings..
if (startPlace<startIndex) {
foundText="";
}
return foundText;
}
/**
* Remove all instances of input string from Output
* @param inputString
* @param removeString
* @return updateString
*/
public static String removeAll(String inputString,
String removeString) {
//Internal Variables
StringBuffer updateString = new StringBuffer();
String tmpString;
for(int a=0; a<inputString.length(); a++ ) {
tmpString = inputString.substring(a,a+1);
if (!tmpString.equals(removeString)) {
updateString.append(tmpString);
}
}
return updateString.toString();
}
/**
* Strip out any trailing characters
* @param inString to be operated on
* @param toRemove string to remove at end if found
* @return inString with end removed
*/
public static String removeTrailing (String inString,
String toRemove) {
while (inString.endsWith(toRemove)) {
inString=inString.substring(0,inString.length()-toRemove.length());
}
return inString;
}
}
Related examples in the same category