Here you can find the source of replaceString(String source, Map args)
public static String replaceString(String source, Map args)
//package com.java2s; /*************************************************************************** * Copyright (c) 2006 Eike Stepper, Fuggerstr. 39, 10777 Berlin, Germany. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * //from w ww . jav a2s . c o m * Contributors: * Eike Stepper - initial API and implementation **************************************************************************/ import java.util.Map; public class Main { /** * Replace string */ public static String replaceString(String source, Map args) { int startIndex = 0; int openIndex = source.indexOf('{', startIndex); if (openIndex == -1) { return source; } int closeIndex = source.indexOf('}', startIndex); if ((closeIndex == -1) || (openIndex > closeIndex)) { return source; } StringBuffer sb = new StringBuffer(); sb.append(source.substring(startIndex, openIndex)); while (true) { String key = source.substring(openIndex + 1, closeIndex); Object val = args.get(key); if (val != null) { sb.append(val); } startIndex = closeIndex + 1; openIndex = source.indexOf('{', startIndex); if (openIndex == -1) { sb.append(source.substring(startIndex)); break; } closeIndex = source.indexOf('}', startIndex); if ((closeIndex == -1) || (openIndex > closeIndex)) { sb.append(source.substring(startIndex)); break; } sb.append(source.substring(startIndex, openIndex)); } return sb.toString(); } /** * Replace string */ public static String replaceString(String source, Object[] args) { int startIndex = 0; int openIndex = source.indexOf('{', startIndex); if (openIndex == -1) { return source; } int closeIndex = source.indexOf('}', startIndex); if ((closeIndex == -1) || (openIndex > closeIndex)) { return source; } StringBuffer sb = new StringBuffer(); sb.append(source.substring(startIndex, openIndex)); while (true) { String intStr = source.substring(openIndex + 1, closeIndex); int index = Integer.parseInt(intStr); sb.append(args[index]); startIndex = closeIndex + 1; openIndex = source.indexOf('{', startIndex); if (openIndex == -1) { sb.append(source.substring(startIndex)); break; } closeIndex = source.indexOf('}', startIndex); if ((closeIndex == -1) || (openIndex > closeIndex)) { sb.append(source.substring(startIndex)); break; } sb.append(source.substring(startIndex, openIndex)); } return sb.toString(); } /** * This is a string replacement method. */ public static String replaceString(String source, String oldStr, String newStr) { StringBuffer sb = new StringBuffer(source.length()); int sind = 0; int cind = 0; while ((cind = source.indexOf(oldStr, sind)) != -1) { sb.append(source.substring(sind, cind)); sb.append(newStr); sind = cind + oldStr.length(); } sb.append(source.substring(sind)); return sb.toString(); } }