Java String Substritute substituteText(String text, String token, String substitute)

Here you can find the source of substituteText(String text, String token, String substitute)

Description

Renders a string based on text where any occurence of token is replaced by substitute.

License

Open Source License

Return

String

Declaration

public static String substituteText(String text, String token, String substitute) 

Method Source Code

//package com.java2s;
/*/*from www.j ava2s  .  co m*/
 *  Util in org.jpws.front.util
 *  file: Util.java
 * 
 *  Project Jpws-Front
 *  @author Wolfgang Keller
 *  Created 28.09.2004
 *  Version
 * 
 *  Copyright (c) 2005 by Wolfgang Keller, Munich, Germany
 * 
 This program is not freeware software but copyright protected to the author(s)
 stated above. However, you can use, redistribute and/or modify it under the terms 
 of the GNU General Public License as published by the Free Software Foundation, 
 version 2 of the License.
    
 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, or go to
 http://www.gnu.org/copyleft/gpl.html.
 */

public class Main {
    /** Renders a string based on <code>text</code> where any occurence of
     *  <code>token</code> is replaced by <code>substitute</code>.
     *  @return String
     */
    public static String substituteText(String text, String token, String substitute) {
        int index;

        if (token == null || substitute == null || (index = text.indexOf(token)) < 0)
            return text;

        while (index > -1) {
            text = text.substring(0, index) + substitute + text.substring(index + token.length());
            index = text.indexOf(token);
        }
        return text;
    }
}

Related

  1. substituteSpaces(String originalPath)
  2. substituteSubString(String input, String find, String replace)
  3. substituteSymbol(String text, String symbol, String value)
  4. substituteSymbol(String text, String symbol, String value)
  5. substituteTabsAndNewLinesWithSpaces(String str)
  6. substituteToken(StringBuffer buf, String token, String value)
  7. substituteUserId(final long aUserId, final String aTemplate)
  8. substituteVars(String text, char[] names, Object[] values)
  9. substituteWhenEmpty(Object value, String valueWhenEmpty)