public static String toFormat(String str, Object... args)
throws IllegalAccessException {
String b = "";
List<String> liststr = new ArrayList<String>();
int oldindex = 0;
int newindex = 0;
for (int i = 0; newindex != -1; i++) {
String of = String.valueOf(i);
newindex = str.indexOf("{" + i + "}");
if (newindex == -1)
break;
liststr.add(str.substring(oldindex, newindex));
oldindex = newindex + of.length() + 2; }
if (liststr.size() != args.length)
throw new IllegalArgumentException("参数个数不匹配"); for (int i = 0; i < args.length; i++) {
b += liststr.get(i) + args[i].toString();
}
return b;
}按自己的想法写的也不知道对不对 请各位大侠指教

解决方案 »

  1.   

    使用方式:String a=StringxtoFormat("我是{0}请各位{1}多多指教","菜鸟","大侠");
      

  2.   

    String a = new String();
    String b = a.format("%c",97);
    System.out.println(b);
      

  3.   

    话说...java本身就有这个函数...不必c的format函数弱
    你这个只是简单的替换位置,应用面太窄了
      

  4.   

    有想法是不错的,  楼主可以看看java自带的吧,  MessageFormat.format()