Advertisement
|
This page contains an archived post to the Java Answers Forum made prior to February 25, 2002.
If you wish to participate in discussions, please visit the new
Artima Forums.
Message:
You have your BASIC ways...
Posted by Kishori Sharan on September 29, 2000 at 11:25 AM
Hi I have created a simple java program to make you feel you are working in BASIC. It has Left, Right and Mid methods and they are static . So if you want to use then just call then as StringUtil.Left ( "Hello", 2 ) ; and it will return you "He" . Thanx Kishori //////////////// StringUtil.java //////////////////////// class StringUtil { public static void main(String[] args) throws Exception { String s = "kishori" ; System.out.println ( " ************ Left( " + s + ", int ) ************* " ); System.out.println ( "Left ( s, -10 ) :" + Left ( s, -10 ) ); System.out.println ( "Left ( s, 0 ) :" + Left ( s, 0 ) ); System.out.println ( "Left ( s, 1 ) :" + Left ( s, 1 ) ); System.out.println ( "Left ( s, 2 ) :" + Left ( s, 2 ) ); System.out.println ( "Left ( s, 4 ) :" + Left ( s, 4 ) ); System.out.println ( "Left ( s, 7 ) :" + Left ( s, 7 ) ); System.out.println ( "Left ( s, 10 ) :" + Left ( s, 10 ) ); System.out.println ( " ************ Right( " + s + ", int ) ************* " ); System.out.println ( "Right ( s, -10 ) :" + Right( s, -10 ) ); System.out.println ( "Right ( s, 0 ) :" + Right ( s, 0 ) ); System.out.println ( "Right ( s, 1 ) :" + Right ( s, 1 ) ); System.out.println ( "Right ( s, 2 ) :" + Right ( s, 2 ) ); System.out.println ( "Right ( s, 4 ) :" + Right ( s, 4 ) ); System.out.println ( "Right ( s, 7 ) :" + Right ( s, 7 ) ); System.out.println ( "Right ( s, 10 ) :" + Right ( s, 10 ) ); System.out.println ( " ************ Mid( " + s + ", int, int ) ************* " ); System.out.println ( "Mid ( s, -10, 10 ) :" + Mid( s, -10, 10 ) ); System.out.println ( "Mid ( s, 0, 3 ) :" + Mid ( s, 0, 3 ) ); System.out.println ( "Mid ( s, 1, 4 ) :" + Mid ( s, 1, 4 ) ); System.out.println ( "Mid ( s, 4, 3 ) :" + Mid ( s, 4, 3) ); System.out.println ( "Mid ( s, 4, 4 ) :" + Mid ( s, 4, 4 ) ); System.out.println ( "Mid ( s, 2, 10 ) :" + Mid ( s, 2, 10 ) ); System.out.println ( "Mid ( s, 4, -1 ) :" + Mid ( s, 4, -1 ) ); System.out.println ( "Mid ( s, 7, 2 ) :" + Mid ( s, 7, 2 ) ); System.out.println ( "Mid ( s, -10, -10 ) :" + Mid ( s, -10, 10 ) ); } public static String Right ( String str, int total ) { String right = null ; // Make sure the paramenter passed is not null if ( str == null ) { return null; } // if total chars from right is zero/less than zero then return empty string if ( total <= 0 ) { return "" ; } // If length of string passed is less than or equal to the // no. of chars wanted from left then just return the parameter itself if ( str.length ( ) <= total ) { return str ; } // Take out the chars wanted from right int beginIndex = str.length ( ) - total ; right = str.substring ( beginIndex ) ; return right ; } public static String Left ( String str, int total ) { String left = null ; // Make sure the paramenter passed is not null if ( str == null ) { return null; } // if total chars from left is zero/less than zero then return empty string if ( total <= 0 ) { return "" ; } // If length of parameter passed is less than or equal to the // no. of chars wanted from left then just return the parameter itself if ( str.length ( ) <= total ) { return str ; } // Take out the chars wanted left = str.substring ( 0, total ) ; return left ; } public static String Mid ( String str, int startPos, int total ) { String mid = null ; // Decrement the startPos by one, we will do all calculations for strings // starting at index zero startPos-- ; // Make sure the paramenter passed is not null if ( str == null ) { return null; } // if total chars from right is zero/less than zero then return empty string if ( total <= 0 ) { return "" ; } int len = str.length ( ) ; // Make sure the startPos is not negative or greater than the length of string if ( startPos < 0 || startPos > len - 1 ) { return "" ; } // Calculate the end pos to be used in substring () method int endPos = startPos + total ; // Validate the endPos value if ( endPos > len ) { mid = str.substring ( startPos ) ; } else { mid = str.substring ( startPos, endPos ) ; } return mid ; } }
Replies:
|