Substitute sub-strings in side of a string : 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 
Substitute sub-strings in side of a string
  

import java.util.Map;


/*
  * JBoss, Home of Professional Open Source
  * Copyright 2005, JBoss Inc., and individual contributors as indicated
  * by the @authors tag. See the copyright.txt in the distribution for a
  * full listing of individual contributors.
  *
  * This is free software; you can redistribute it and/or modify it
  * under the terms of the GNU Lesser General Public License as
  * published by the Free Software Foundation; either version 2.1 of
  * the License, or (at your option) any later version.
  *
  * This software 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
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this software; if not, write to the Free
  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  */



public class Main{
  /** An empty string constant */
  public static final String EMPTY = "";

  /////////////////////////////////////////////////////////////////////////
  //                         Substitution Methods                        //
  /////////////////////////////////////////////////////////////////////////

  /**
   * Substitute sub-strings in side of a string.
   *
   @param buff    Stirng buffer to use for substitution (buffer is not reset)
   @param from    String to substitute from
   @param to      String to substitute to
   @param string  String to look for from in
   @return        Substituted string
   */
  public static String subst(final StringBuffer buff, final String from,
     final String to, final String string)
  {
     int begin 0end 0;

     while ((end = string.indexOf(from, end)) != -1)
     {
        // append the first part of the string
        buff.append(string.substring(begin, end));

        // append the replaced string
        buff.append(to);

        // update positions
        begin end from.length();
        end begin;
     }

     // append the rest of the string
     buff.append(string.substring(begin, string.length()));

     return buff.toString();
  }

  /**
   * Substitute sub-strings in side of a string.
   *
   @param from    String to substitute from
   @param to      String to substitute to
   @param string  String to look for from in
   @return        Substituted string
   */
  public static String subst(final String from, final String to,
     final String string)
  {
     return subst(new StringBuffer(), from, to, string);
  }

  /**
   * Substitute sub-strings in side of a string.
   *
   @param buff       String buffer to use for substitution (buffer is not reset)
   @param string     String to subst mappings in
   @param map        Map of from->to strings
   @param beginToken Beginning token
   @param endToken   Ending token
   @return           Substituted string
   */
  public static String subst(final StringBuffer buff, final String string,
     final Map map, final String beginToken,
     final String endToken)
  {
     int begin 0, rangeEnd = 0;
     Range range;

     while ((range = rangeOf(beginToken, endToken, string, rangeEnd)) != null)
     {
        // append the first part of the string
        buff.append(string.substring(begin, range.begin));

        // Get the string to replace from the map
        String key = string.substring(range.begin + beginToken.length(),
           range.end);
        Object value = map.get(key);
        // if mapping does not exist then use empty;
        if (value == nullvalue = EMPTY;

        // append the replaced string
        buff.append(value);

        // update positions
        begin = range.end + endToken.length();
        rangeEnd = begin;
     }

     // append the rest of the string
     buff.append(string.substring(begin, string.length()));

     return buff.toString();
  }

  /**
   * Substitute sub-strings in side of a string.
   *
   @param string     String to subst mappings in
   @param map        Map of from->to strings
   @param beginToken Beginning token
   @param endToken   Ending token
   @return           Substituted string
   */
  public static String subst(final String string, final Map map,
     final String beginToken, final String endToken)
  {
     return subst(new StringBuffer(), string, map, beginToken, endToken);
  }

  /**
   * Substitute index identifiers with the replacement value from the
   * given array for the corresponding index.
   *
   @param buff       The string buffer used for the substitution
   *                   (buffer is not reset).
   @param string     String substitution format.
   @param replace    Array of strings whose values will be used as 
   *                   replacements in the given string when a token with
   *                   their index is found.
   @param token      The character token to specify the start of an index
   *                   reference.
   @return           Substituted string.
   */
  public static String subst(final StringBuffer buff, final String string,
     final String replace[]final char token)
  {
     int i = string.length();
     for (int j = 0; j >= && j < i; j++)
     {
        char c = string.charAt(j);

        // if the char is the token, then get the index
        if (c == token)
        {

           // if we aren't at the end of the string, get the index
           if (j != i)
           {
              int k = Character.digit(string.charAt(j + 1)10);

              if (k == -1)
              {
                 buff.append(string.charAt(j + 1));
              }
              else if (k < replace.length)
              {
                 buff.append(replace[k]);
              }

              j++;
           }
        }
        else
        {
           buff.append(c);
        }
     }

     return buff.toString();
  }

  /**
   * Substitute index identifiers with the replacement value from the
   * given array for the corresponding index.
   *
   @param string     String substitution format.
   @param replace    Array of strings whose values will be used as 
   *                   replacements in the given string when a token with
   *                   their index is found.
   @param token      The character token to specify the start of an index
   *                   reference.
   @return           Substituted string.
   */
  public static String subst(final String string, final String replace[],
     final char token)
  {
     return subst(new StringBuffer(), string, replace, token);
  }

  /**
   * Substitute index identifiers (with <code>%</code> for the index token)
   * with the replacement value from the given array for the corresponding
   * index.
   *
   @param string     String substitution format.
   @param replace    Array of strings whose values will be used as 
   *                   replacements in the given string when a token with
   *                   their index is found.
   @return           Substituted string.
   */
  public static String subst(final String string, final String replace[])
  {
     return subst(new StringBuffer(), string, replace, '%');
  }

  /////////////////////////////////////////////////////////////////////////
  //                             Range Methods                           //
  /////////////////////////////////////////////////////////////////////////

  /**
   * Represents a range between two integers.
   */
  public static class Range
  {
     /** The beginning of the range. */
     public int begin;

     /** The end of the range. */
     public int end;

     /**
      * Construct a new range.
      *
      @param begin   The beginning of the range.
      @param end     The end of the range.
      */
     public Range(int begin, int end)
     {
        this.begin begin;
        this.end end;
     }

     /**
      * Default constructor.
      */
     public Range()
     {
     }
  }

  /**
   * Return the range from a begining token to an ending token.
   *
   @param beginToken String to indicate begining of range.
   @param endToken   String to indicate ending of range.
   @param string     String to look for range in.
   @param fromIndex  Beginning index.
   @return           (begin index, end index) or <i>null</i>.
   */
  public static Range rangeOf(final String beginToken, final String endToken,
     final String string, final int fromIndex)
  {
     int begin = string.indexOf(beginToken, fromIndex);

     if (begin != -1)
     {
        int end = string.indexOf(endToken, begin 1);
        if (end != -1)
        {
           return new Range(begin, end);
        }
     }

     return null;
  }

  /**
   * Return the range from a begining token to an ending token.
   *
   @param beginToken String to indicate begining of range.
   @param endToken   String to indicate ending of range.
   @param string     String to look for range in.
   @return           (begin index, end index) or <i>null</i>.
   */
  public static Range rangeOf(final String beginToken, final String endToken,
     final String string)
  {
     return rangeOf(beginToken, endToken, string, 0);
  }

}

   
    
  
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. Find, replace and remove strings
22. Java Implementation of Pythons string.replace()
23. Replace New Lines
24. Replace string
25. Replace strings
26. Replace substrings within string
27. Replaces all occurences of a substring inside a string
28. Replaces each substring of this string that matches toReplace
29. Replaces substrings
30. Replaces all instances of oldString with newString in 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.