KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > protomatter > util > StringUtil


1 package com.protomatter.util;
2
3 /**
4  * {{{ The Protomatter Software License, Version 1.0
5  * derived from The Apache Software License, Version 1.1
6  *
7  * Copyright (c) 1998-2002 Nate Sammons. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution,
22  * if any, must include the following acknowledgment:
23  * "This product includes software developed for the
24  * Protomatter Software Project
25  * (http://protomatter.sourceforge.net/)."
26  * Alternately, this acknowledgment may appear in the software itself,
27  * if and wherever such third-party acknowledgments normally appear.
28  *
29  * 4. The names "Protomatter" and "Protomatter Software Project" must
30  * not be used to endorse or promote products derived from this
31  * software without prior written permission. For written
32  * permission, please contact support@protomatter.com.
33  *
34  * 5. Products derived from this software may not be called "Protomatter",
35  * nor may "Protomatter" appear in their name, without prior written
36  * permission of the Protomatter Software Project
37  * (support@protomatter.com).
38  *
39  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
40  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
41  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
42  * DISCLAIMED. IN NO EVENT SHALL THE PROTOMATTER SOFTWARE PROJECT OR
43  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
45  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
46  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
47  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
48  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
49  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50  * SUCH DAMAGE. }}}
51  */

52
53 import java.util.*;
54
55 /**
56  * Utility methods for manipulating strings.
57  */

58 public class StringUtil
59 {
60   /**
61    * Private constructor so nobody constructs us.
62    */

63   private StringUtil()
64   {
65     super();
66   }
67
68   /**
69    * Makes sure that a string has no more than n characters,
70    * and pads it with spaces if it does.
71    */

72   public static void pad(StringBuffer JavaDoc b, String JavaDoc s, int n)
73   {
74     int l = s.length();
75     if (l == n)
76     {
77       b.append(s);
78       return;
79     }
80     else if (l > n)
81     {
82       b.append(s.substring(0, n));
83       return;
84     }
85     else
86     {
87       b.append(s.substring(0, l));
88       while (l++<n)
89         b.append(' ');
90       return;
91     }
92   }
93
94   /**
95    * Makes sure that a string has no more than n characters,
96    * and pads it with spaces if it does.
97    */

98   public static String JavaDoc pad(String JavaDoc s, int n)
99   {
100     int l = s.length();
101     if (l == n)
102      return s;
103     else if (l > n)
104      return s.substring(0, n);
105     else
106     {
107       char[] carr = new char[n];
108       s.getChars(0, l, carr, 0);
109       while (l<n)
110         carr[l++] = ' ';
111       return new String JavaDoc(carr);
112     }
113   }
114
115   /**
116    * Truncate a string to the given length.
117    * If s.length() <= n, returns s.
118    * Else, returns the first n characters of s.
119    */

120   public static String JavaDoc truncate(String JavaDoc s, int n)
121   {
122     if (s.length() <= n)
123       return s;
124     else
125       return s.substring(0, n);
126   }
127
128   /**
129    * Nicely truncate a string.
130    * Truncates s to n chars breaking s on whitespace,
131    * and adding a "..." to the end.
132    */

133   public static String JavaDoc truncateNicely(String JavaDoc s, int n)
134   {
135     if (n <= 3)
136     {
137       StringBuffer JavaDoc b = new StringBuffer JavaDoc(n);
138       for (int i=0; i<n; i++)
139         b.append(".");
140       return b.toString();
141     }
142     int sublen = n-3;
143     StringBuffer JavaDoc clippedtext = new StringBuffer JavaDoc();
144     StringTokenizer st = new StringTokenizer(s);
145     while (st.hasMoreTokens())
146     {
147       String JavaDoc word = st.nextToken();
148       int cliplen = clippedtext.length();
149       if(word.length() + cliplen + 1 <= sublen)
150       {
151         if (cliplen == 0)
152         {
153           clippedtext.append(word);
154         }
155         else
156         {
157           clippedtext.append(" ");
158           clippedtext.append(word);
159         }
160       }
161       else
162       {
163         if(clippedtext.length() == 0)
164           clippedtext.append(s.substring(0,sublen));
165         clippedtext.append("...");
166         break;
167       }
168     }
169     return clippedtext.toString();
170   }
171
172   public static String JavaDoc replace(String JavaDoc inputString, String JavaDoc token, String JavaDoc replacement)
173   {
174       int index = 0;
175       int replacementLength = replacement.length();
176       int tokenLength = token.length();
177       String JavaDoc input = inputString;
178       while (true)
179       {
180           index = input.indexOf(token, index);
181           if (index == -1)
182           {
183               return input;
184           }
185           StringBuffer JavaDoc buf = new StringBuffer JavaDoc(input.length() + replacementLength);
186           buf.append(input.substring(0, index));
187           buf.append(replacement);
188           buf.append(input.substring(index + tokenLength));
189           input = buf.toString();
190       }
191   }
192 }
193
Popular Tags