KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > bsh > StringUtil


1 /*****************************************************************************
2  * *
3  * This file is part of the BeanShell Java Scripting distribution. *
4  * Documentation and updates may be found at http://www.beanshell.org/ *
5  * *
6  * Sun Public License Notice: *
7  * *
8  * The contents of this file are subject to the Sun Public License Version *
9  * 1.0 (the "License"); you may not use this file except in compliance with *
10  * the License. A copy of the License is available at http://www.sun.com *
11  * *
12  * The Original Code is BeanShell. The Initial Developer of the Original *
13  * Code is Pat Niemeyer. Portions created by Pat Niemeyer are Copyright *
14  * (C) 2000. All Rights Reserved. *
15  * *
16  * GNU Public License Notice: *
17  * *
18  * Alternatively, the contents of this file may be used under the terms of *
19  * the GNU Lesser General Public License (the "LGPL"), in which case the *
20  * provisions of LGPL are applicable instead of those above. If you wish to *
21  * allow use of your version of this file only under the terms of the LGPL *
22  * and not to allow others to use your version of this file under the SPL, *
23  * indicate your decision by deleting the provisions above and replace *
24  * them with the notice and other provisions required by the LGPL. If you *
25  * do not delete the provisions above, a recipient may use your version of *
26  * this file under either the SPL or the LGPL. *
27  * *
28  * Patrick Niemeyer (pat@pat.net) *
29  * Author of Learning Java, O'Reilly & Associates *
30  * http://www.pat.net/~pat/ *
31  * *
32  *****************************************************************************/

33
34 package bsh;
35
36 import java.util.*;
37
38 public class StringUtil {
39
40     public static String JavaDoc [] split( String JavaDoc s, String JavaDoc delim) {
41         Vector v = new Vector();
42         StringTokenizer st = new StringTokenizer(s, delim);
43         while ( st.hasMoreTokens() )
44             v.addElement( st.nextToken() );
45         String JavaDoc [] sa = new String JavaDoc [ v.size() ];
46         v.copyInto( sa );
47         return sa;
48     }
49
50     public static String JavaDoc [] bubbleSort( String JavaDoc [] in ) {
51         Vector v = new Vector();
52         for(int i=0; i<in.length; i++)
53             v.addElement(in[i]);
54
55         int n = v.size();
56         boolean swap = true;
57         while ( swap ) {
58             swap = false;
59             for(int i=0; i<(n-1); i++)
60                 if ( ((String JavaDoc)v.elementAt(i)).compareTo(
61                         ((String JavaDoc)v.elementAt(i+1)) ) > 0 ) {
62                     String JavaDoc tmp = (String JavaDoc)v.elementAt(i+1);
63                     v.removeElementAt( i+1 );
64                     v.insertElementAt( tmp, i );
65                     swap = true;
66                 }
67         }
68
69         String JavaDoc [] out = new String JavaDoc [ n ];
70         v.copyInto(out);
71         return out;
72     }
73
74
75     public static String JavaDoc maxCommonPrefix( String JavaDoc one, String JavaDoc two ) {
76         int i=0;
77         while( one.regionMatches( 0, two, 0, i ) )
78             i++;
79         return one.substring(0, i-1);
80     }
81
82     public static String JavaDoc methodString(String JavaDoc name, Class JavaDoc[] types)
83     {
84         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(name + "(");
85         if ( types.length > 0 )
86             sb.append(" ");
87         for( int i=0; i<types.length; i++ )
88         {
89             Class JavaDoc c = types[i];
90             sb.append( ( (c == null) ? "null" : c.getName() )
91                 + ( i < (types.length-1) ? ", " : " " ) );
92         }
93         sb.append(")");
94         return sb.toString();
95     }
96
97     /**
98         Split a filename into dirName, baseName
99         @return String [] { dirName, baseName }
100     public String [] splitFileName( String fileName )
101     {
102         String dirName, baseName;
103         int i = fileName.lastIndexOf( File.separator );
104         if ( i != -1 ) {
105             dirName = fileName.substring(0, i);
106             baseName = fileName.substring(i+1);
107         } else
108             baseName = fileName;
109
110         return new String[] { dirName, baseName };
111     }
112
113     */

114
115     /**
116         Hack - The real method is in Reflect.java which is not public.
117     */

118     public static String JavaDoc normalizeClassName( Class JavaDoc type )
119     {
120         return Reflect.normalizeClassName( type );
121     }
122 }
123
Popular Tags