KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > etymon > pj > util > StringUtil


1 package com.etymon.pj.util;
2
3 public final class StringUtil {
4
5     public static String JavaDoc sprintf(String JavaDoc template, String JavaDoc[] args) {
6         if (args == null) {
7             return template;
8         }
9         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
10         int start = 0;
11         int index;
12         int x = 0;
13         while ( (x < args.length) && ((index =
14                            template.indexOf("%s",
15                                 start)) != -1) ) {
16             // allow \%s to escape
17
if ( (index == 0) || (template.charAt(index -
18                                   1) !=
19                           '\\') ) {
20                 sb.append(template.substring(start,
21                                  index));
22                 sb.append(args[x]);
23                 start = index + 2;
24             }
25         }
26         sb.append(template.substring(start));
27         return sb.toString();
28     }
29
30 }
31
Popular Tags