Find, replace and remove strings : String replace « Data Type « Java






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

1.String Replace
2.String Replace 2String Replace 2
3.Replace \r\n with the
tag
4.Unaccent letters
5.Remove leading white space from a string
6.Remove leading whitespace a String using regular expressions
7.Remove trailing whitespace
8.Replace multiple whitespaces between words with single blank
9.Replacing Characters in a String: replace() method creates a new string with the replaced characters.
10.Replacing Substrings in a String
11.Replace characters in string
12.String.replaceAll() can be used with String
13.Replaces all occurrences of given character with new one and returns new String object.
14.Replaces only first occurrences of given String with new one and returns new String object.
15.Replaces all occurrences of given String with new one and returns new String object.
16.Only replace first occurence
17.Optimize String operations
18.Replace every occurrences of a string within a string
19.Replace/remove character in a String: replace all occurrences of a given character
20.To replace a character at a specified position
21.Java Implementation of Pythons string.replace()
22.Replace New Lines
23.Replace string
24.Replace strings
25.Replace substrings within string
26.Replaces all occurences of a substring inside a string
27.Replaces each substring of this string that matches toReplace
28.Replaces substrings
29.Replaces all instances of oldString with newString in string
30.Substitute sub-strings in side of a string
31.Replace string
32.Returns the given string with all found oldSubStr replaced by newSubStr.
33.Returns a new string resulting from replacing all occurrences of oldStr in this string with newStr.
34.Find and replace all occurrences of the string