Here you can find the source of formatString(String stringFormat, String... args)
Parameter | Description |
---|---|
stringFormat | a parameter |
args | a parameter |
public static String formatString(String stringFormat, String... args)
//package com.java2s; //License from project: Open Source License import java.util.ArrayList; import java.util.List; public class Main { /**/* w w w . j a v a 2s. c o m*/ * Format string such like "Input params {0} could be {1} " * @param stringFormat * @param args * @return */ public static String formatString(String stringFormat, String... args) { if (args.length == 0) { return stringFormat; } else { List<String> listArgs = new ArrayList<String>(); for (String arg : args) { listArgs.add(arg); } StringBuilder sb = new StringBuilder(stringFormat); int beginIndex = 0; int endIndex = 0; int argIndex = 0; for (int index = 0; index < sb.length();) { beginIndex = sb.indexOf("{", index); if (beginIndex < 0) { break; } endIndex = sb.indexOf("}", beginIndex); if (endIndex < 0) { break; } argIndex = Integer.parseInt(sb.substring(beginIndex + 1, endIndex)); sb.replace(beginIndex, endIndex + 1, listArgs.get(argIndex)); index = beginIndex + listArgs.get(argIndex).length(); } return sb.toString(); } } }