001         package com.croftsoft.core.lang;
002    
003         import java.io.*;
004         import java.util.*;
005    
006         /*********************************************************************
007         * A collection of static methods to manipulate java.lang.String.
008         * 
009         * @version
010         *   2001-08-15
011         * @since
012         *   1998-10-04
013         * @author
014         *   <a href="https://www.croftsoft.com/">David Wallace Croft</a>
015         *********************************************************************/
016    
017         public class  StringLib
018         //////////////////////////////////////////////////////////////////////
019         //////////////////////////////////////////////////////////////////////
020         {
021    
022         public static final String [ ]  ZERO_LENGTH_STRING_ARRAY
023           = new String [ ] { };
024    
025         //////////////////////////////////////////////////////////////////////
026         //////////////////////////////////////////////////////////////////////
027    
028         public static void  main ( String [ ]  args )
029         //////////////////////////////////////////////////////////////////////
030         {
031           System.out.println ( replace ( "0", "0", "12345" ) );
032           System.out.println ( replace ( "1abc5", "abc", "234" ) );
033           System.out.println ( replace ( "1a", "a", "2345" ) );
034           System.out.println ( replace ( "1235", "23", "234" ) );
035         }
036    
037         /*********************************************************************
038         * @see #padRight
039         *********************************************************************/
040         public static String  padLeft ( String  s, char  c, int  length )
041         //////////////////////////////////////////////////////////////////////
042         {
043           if ( s == null ) return null;
044           if ( s.length ( ) >= length ) return new String ( s );
045           while ( s.length ( ) < length ) s = c + s;
046           return s;
047         }
048    
049         /*********************************************************************
050         * @see #padLeft
051         *********************************************************************/
052         public static String  padRight ( String  s, char  c, int  length )
053         //////////////////////////////////////////////////////////////////////
054         {
055           if ( s == null ) return null;
056           if ( s.length ( ) >= length ) return new String ( s );
057           while ( s.length ( ) < length ) s = s + c;
058           return s;
059         }
060    
061         /*********************************************************************
062         * Returns a String with the characters between
063         * <I>fromIndex</I> and <I>toIndex</I> removed, inclusive.
064         *********************************************************************/
065         public static String  remove (
066           String  s, int  fromIndex, int  toIndex )
067         //////////////////////////////////////////////////////////////////////
068         {
069           return s.substring ( 0, fromIndex ) + s.substring ( toIndex + 1 );
070         }
071    
072         /*********************************************************************
073         * Replaces substrings in a String.
074         * Replaces every instance of <I>oldSub</I> in <I>s</I> with
075         *   <I>newSub</I>.
076         *********************************************************************/
077         public static String  replace (
078           String  s, String  oldSub, String  newSub )
079         //////////////////////////////////////////////////////////////////////
080         // When Java 1.2 comes out, use StringBuffer.replace().
081         //////////////////////////////////////////////////////////////////////
082         {
083           StringBuffer  sb = new StringBuffer ( );
084           int  index;
085           int  remnantIndex = 0;
086           while ( ( index = s.indexOf ( oldSub, remnantIndex ) ) > -1 )
087           {
088             if ( index > remnantIndex )
089             {
090               sb.append ( s.substring ( remnantIndex, index ) );
091             }
092             sb.append ( newSub );
093             remnantIndex = index + oldSub.length ( );
094           }
095           sb.append ( s.substring ( remnantIndex ) );
096           return sb.toString ( );
097         }
098    
099         /*********************************************************************
100         * Breaks a String at the ends of lines.
101         * The original text is broken into an array of String at the
102         * end-of-line delimiters such as carriage return, linefeed, or the
103         * carriage return/linefeed pair.
104         * This is returned as an array of String without the delimiters.
105         *********************************************************************/
106         public static String [ ]  toStringArray ( String  text )
107         //////////////////////////////////////////////////////////////////////
108         {
109           Vector  stringVector = new Vector ( );
110    
111           BufferedReader  bufferedReader
112             = new BufferedReader ( new StringReader ( text ) );
113    
114           String  line;
115    
116           try
117           {
118             while ( ( line = bufferedReader.readLine ( ) ) != null )
119             {
120               stringVector.addElement ( line );
121             }
122           }
123           catch ( IOException  ex )
124           {
125             // this should never happen
126           }
127    
128           String [ ]  stringArray = new String [ stringVector.size ( ) ];
129    
130           stringVector.copyInto ( stringArray );
131    
132           return stringArray;
133         }
134    
135         /*********************************************************************
136         * Returns null if the trimming operation results in the empty String.
137         *
138         * @param  s
139         *
140         *   May be null.
141         *
142         * @return
143         *
144         *   Returns the argument trimmed or null if the argument is null
145         *   or the argument trimmed is "".
146         *********************************************************************/
147         public static String  trimToNull ( String  s )
148         //////////////////////////////////////////////////////////////////////
149         {
150           if ( s == null )
151           {
152             return null;
153           }
154    
155           s = s.trim ( );
156    
157           return "".equals ( s ) ? null : s;
158         }
159    
160         //////////////////////////////////////////////////////////////////////
161         //////////////////////////////////////////////////////////////////////
162    
163         private  StringLib ( ) { }
164    
165         //////////////////////////////////////////////////////////////////////
166         //////////////////////////////////////////////////////////////////////
167         }