KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > util > Strip


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10 package org.mmbase.util;
11
12 import org.mmbase.util.logging.Logger;
13 import org.mmbase.util.logging.Logging;
14
15 /**
16  * Class to strip characters from the beginning and end of strings.
17  *
18  * <PRE>
19  * Example1: Strip.Char("..dfld..",'.',Strip.TRAILING) yields "..dlfd."
20  * Example2: Strip.Chars("..dfld..",".",Strip.TRAILING) yields "..dlfd"
21  * Example3: Strip.Chars(". .. dfld. , .","., ",Strip.BOTH) yields "dfld"
22  * </PRE>
23  *
24  * @author Rico Jansen
25  * @version $Id: Strip.java,v 1.7 2004/09/30 14:07:13 pierre Exp $
26  */

27 public class Strip {
28
29     // logger
30
private static Logger log = Logging.getLoggerInstance(Strip.class.getName());
31
32     /**
33      * Strip nothing, a rather ineffecient form of a copy
34      */

35     public static final int NOTHING=0;
36
37     /**
38      * Strip leading, only characters at begin of string are checked
39      */

40     public static final int LEADING=1;
41
42     /**
43      * Strip trailing, only characters at end of string are checked
44      */

45     public static final int TRAILING=2;
46
47     /**
48      * Strip both, characters at begin and end of string are checked
49      */

50     public static final int BOTH=3;
51
52     /**
53      * Strip double quotes from beginning, end or both, only once.
54      * @param str the string to strip
55      * @param where one of {@link #NOTHING}, {@link #LEADING}, {@link #TRAILING}
56      * or {@link #BOTH}
57      * @return the stripped String
58      */

59     public static String JavaDoc DoubleQuote(String JavaDoc str,int where) {
60         return Char(str,'"',where);
61     }
62
63     /**
64      * Strip single quotes from beginning, end or both, only once.
65      * @param str the string to strip
66      * @param where one of {@link #NOTHING}, {@link #LEADING}, {@link #TRAILING}
67      * or {@link #BOTH}
68      * @return the stripped String
69      */

70     public static String JavaDoc SingleQuote(String JavaDoc str,int where) {
71         return Char(str,'\'',where);
72     }
73
74     /**
75      * Strip multiple whitespace characters from beginning, end or both, that
76      * means keep on stripping util a non-whitespace character is found.
77      * @param str the string to strip
78      * @param where one of {@link #NOTHING}, {@link #LEADING}, {@link #TRAILING}
79      * or {@link #BOTH}
80      * @return the stripped String
81      */

82     public static String JavaDoc Whitespace(String JavaDoc str,int where) {
83         return Chars(str," \t\n\r",where);
84     }
85
86     /**
87      * Strip all of the specified character from beginning, end or both.
88      * @param str the string to strip
89      * @param chr the character to strip from the string
90      * @param where one of {@link #NOTHING}, {@link #LEADING}, {@link #TRAILING}
91      * or {@link #BOTH}
92      * @return the stripped String
93      */

94     public static String JavaDoc Char(String JavaDoc str,char chr,int where) {
95         if (str!=null && str.length()>0) {
96             int lead=0;
97             int trail=str.length()-1;
98
99             switch(where) {
100                 case LEADING:
101                     if (str.charAt(lead)==chr) lead++;
102                     break;
103                 case TRAILING:
104                     if (str.charAt(trail)==chr) trail--;
105                     break;
106                 case BOTH:
107                     if (str.charAt(lead)==chr) lead++;
108                     if (str.charAt(trail)==chr) trail--;
109                     break;
110                 default:
111                     break;
112             }
113             str=str.substring(lead,trail+1);
114         }
115         return str;
116     }
117
118     /**
119      * Strip multiple characters contained in the set given as second parameter
120      * until a non-set character.
121      * @param str the string to strip
122      * @param chars a string containing all characters to strip from the string
123      * @param where one of {@link #NOTHING}, {@link #LEADING}, {@link #TRAILING}
124      * or {@link #BOTH}
125      * @return the stripped String
126      */

127     public static String JavaDoc Chars(String JavaDoc str,String JavaDoc chars,int where) {
128
129         if (str!=null && str.length()>0) {
130             int lead=0;
131             int trail=str.length()-1;
132
133             if (trail<1) {
134                 where=LEADING;
135             } else {
136                 switch(where) {
137                     case LEADING:
138                         while(chars.indexOf(str.charAt(lead))!=-1 && (lead<str.length()-1)) lead++;
139                         break;
140                     case TRAILING:
141                         while(chars.lastIndexOf(str.charAt(trail))!=-1 && trail>0) trail--;
142                         break;
143                     case BOTH:
144                         while(chars.indexOf(str.charAt(lead))!=-1 && lead<(str.length()-1)) lead++;
145                         while(chars.lastIndexOf(str.charAt(trail))!=-1 && trail>=lead) trail--;
146                         break;
147                     default:
148                         break;
149                 }
150             }
151             if (lead<=trail) {
152                 str=str.substring(lead,trail+1);
153             } else {
154                 str="";
155             }
156         }
157         return str;
158     }
159
160     /**
161      * Test the class
162      */

163     public static void main(String JavaDoc args[]) {
164         log.info("Double "+Strip.DoubleQuote("\"double\"",Strip.BOTH));
165         log.info("Single "+Strip.SingleQuote("'single'",Strip.BOTH));
166         log.info("White |"+Strip.Whitespace(" white \n",Strip.BOTH)+"|");
167     }
168 }
169
Popular Tags