KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snipsnap > util > StringUtil


1 /*
2  * This file is part of "SnipSnap Wiki/Weblog".
3  *
4  * Copyright (c) 2002 Stephan J. Schmidt, Matthias L. Jugel
5  * All Rights Reserved.
6  *
7  * Please visit http://snipsnap.org/ for updates and contact.
8  *
9  * --LICENSE NOTICE--
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  * --LICENSE NOTICE--
24  */

25 package org.snipsnap.util;
26
27 import java.util.StringTokenizer JavaDoc;
28
29 /**
30  * Helper utility for string handling.
31  *
32  * @author Stephan J. Schmidt
33  * @version $Id: StringUtil.java 853 2003-05-20 09:06:07Z stephan $
34  */

35 public class StringUtil {
36   public static String JavaDoc plural(int i, String JavaDoc s1, String JavaDoc s2) {
37     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
38     return plural(buffer, i, s1, s2).toString();
39   }
40
41   public static String JavaDoc[] split(String JavaDoc string, String JavaDoc delimiter) {
42     //@TODO: use Jakarta Commons lang or JDK1.4
43
StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(string, delimiter);
44     String JavaDoc[] result = new String JavaDoc[tokenizer.countTokens()];
45     int i=0;
46     while(tokenizer.hasMoreTokens()) {
47       result[i++] = tokenizer.nextToken();
48     }
49     return result;
50   }
51
52   public static String JavaDoc plural(int i, String JavaDoc s) {
53     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
54     return plural(buffer, i, s).toString();
55   }
56
57   public static StringBuffer JavaDoc plural(StringBuffer JavaDoc buffer, int i, String JavaDoc s1, String JavaDoc s2) {
58     buffer.append(i);
59     buffer.append(" ");
60     if (i > 1 || i == 0) {
61       buffer.append(s1);
62     } else {
63       buffer.append(s2);
64     }
65     return buffer;
66   }
67
68   public static StringBuffer JavaDoc plural(StringBuffer JavaDoc buffer, int i, String JavaDoc s) {
69     buffer.append(i);
70     buffer.append(" ");
71     buffer.append(s);
72     if (i > 1 || i == 0) {
73       buffer.append("s");
74     }
75     return buffer;
76   }
77 }
78
Popular Tags