1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package net.sf.jour.util;
22
23 import java.util.StringTokenizer;
24 import java.text.DecimalFormat;
25
26 /***
27 * TODO Add docs
28 * Created on 04.12.2004
29 *
30 * Contributing Author(s):
31 *
32 * Misha Lifschitz <mishalifschitz at users.sourceforge.net> (Inital implementation)
33 * Vlad Skarzhevskyy <vlads at users.sourceforge.net> (Inital implementation)
34 *
35 * @author vlads
36 * @version $Revision: 1.3 $ ($Author: vlads $) $Date: 2004/12/15 05:09:26 $
37 */
38 public class StringUtil {
39 /***
40 * jre 1.3 compatible replaceAll.
41 * @param str text to search and replace in
42 * @param replace the String to search for
43 * @param replacement the String to replace with
44 * @return the text with any replacements processed
45 */
46 public static String replaceAll(String str, String replace, String replacement)
47 {
48 StringBuffer sb = new StringBuffer(str);
49 int firstOccurrence = str.indexOf(replace);
50
51 while (firstOccurrence != -1)
52 {
53 sb.replace(firstOccurrence, firstOccurrence + replace.length(), replacement);
54 firstOccurrence = sb.toString().indexOf(replace);
55 }
56
57 return sb.toString();
58 }
59
60 /***
61 * DOCUMENT ME!
62 *
63 * @param str DOCUMENT ME!
64 * @param delim DOCUMENT ME!
65 *
66 * @return DOCUMENT ME!
67 */
68 private String[] stringToArray(String str, String delim) {
69 StringTokenizer paramStrokenizer = new StringTokenizer(str, delim);
70
71 String[] retVal = new String[paramStrokenizer.countTokens()];
72 int i = 0;
73
74 while (paramStrokenizer.hasMoreTokens()) {
75 retVal[i++] = paramStrokenizer.nextToken();
76 }
77
78 return retVal;
79 }
80
81 public static String percent(double duration, double durationTotal) {
82 if (durationTotal == 0.0) {
83 return "n/a";
84 }
85 final DecimalFormat format = new DecimalFormat("##0");
86 format.setMinimumIntegerDigits(1);
87 format.setMaximumFractionDigits(0);
88
89 int prc = (new Double(Math.floor(100 * duration/durationTotal))).intValue();
90 return format.format(prc);
91 }
92 }