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

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. EJB3
14. Email
15. Event
16. File Input Output
17. Game
18. Generics
19. GWT
20. Hibernate
21. I18N
22. J2EE
23. J2ME
24. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » Data Type » String replaceScreenshots 
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 replaceAllString 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
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.