Is there a method to delete characters?
No — only methods that add characters at the end or change selected characters.
There is a reverse()
method in class StringBuffer
that reverses the order of the characters.
Let us conveniently forget about that method and re-write the example
program to do the same thing:
public class ReverseTester { public static String reverse( String data ) { StringBuffer temp = new StringBuffer(); for ( int j=data.length()-1; j >= 0; j-- ) temp.append( data.charAt(j) ); return temp.toString(); } public static void main ( String[] args ) { if ( args.length > 0 ) System.out.println( reverse( args[0] ) ); } }
In this version of reverse()
, only two new objects
are created: the StringBuffer
and the returned
String
object.