یه نمونش تو این دوتا مثال هست:
ببخشید بلد نبودم چجوری کد رو بذارم!
از اینجا هم میتونین دانلود کنین. 15Mg (بهتره از دانلود منیجر استفاده نکنین)
دایتل فصل 16
// Fig. 16.13: StringBuilderAppend.java
// StringBuilder append methods.
public class StringBuilderAppend
{
public static void main( String[] args )
{
Object objectRef = "hello";
String string = "goodbye";
char[] charArray = { 'a', 'b', 'c', 'd', 'e', 'f' };
boolean booleanValue = true;
char characterValue = 'Z';
int integerValue = 7;
long longValue = 10000000000L;
float floatValue = 2.5f;
double doubleValue = 33.333;
StringBuilder lastBuffer = new StringBuilder( "last buffer" );
StringBuilder buffer = new StringBuilder();
buffer.append( "\n" );
buffer.append( string );
buffer.append( "\n" );
buffer.append( charArray );
buffer.append( "\n" );
buffer.append( charArray, 0, 3 );
buffer.append( "\n" );
buffer.append( booleanValue );
buffer.append( "\n" );
buffer.append( characterValue );
buffer.append( "\n" );
buffer.append( integerValue );
buffer.append( "\n" );
buffer.append( longValue );
buffer.append( "\n" );
buffer.append( floatValue );
buffer.append( "\n" );
buffer.append( doubleValue );
buffer.append( "\n" );
buffer.append( lastBuffer );
System.out.printf( "buffer contains %s\n", buffer.toString() );
} // end main
} // end StringBuilderAppend
دومی:
// Fig. 16.14: StringBuilderInsertDelete.java
// StringBuilder methods insert, delete and deleteCharAt.
public class StringBuilderInsertDelete
{
public static void main( String[] args )
{
Object objectRef = "hello";
String string = "goodbye";
char[] charArray = { 'a', 'b', 'c', 'd', 'e', 'f' };
boolean booleanValue = true;
char characterValue = 'K';
int integerValue = 7;
long longValue = 10000000;
float floatValue = 2.5f; // f suffix indicates that 2.5 is a float
double doubleValue = 33.333;
StringBuilder buffer = new StringBuilder();
buffer.insert( 0, objectRef );
buffer.insert( 0, " " ); // each of these contains two spaces
buffer.insert( 0, string );
buffer.insert( 0, " " );
buffer.insert( 0, charArray );
buffer.insert( 0, " " );
buffer.insert( 0, charArray, 3, 3 );
buffer.insert( 0, " " );
buffer.insert( 0, booleanValue );
buffer.insert( 0, " " );
buffer.insert( 0, characterValue );
buffer.insert( 0, " " );
buffer.insert( 0, integerValue );
buffer.insert( 0, " " );
buffer.insert( 0, longValue );
buffer.insert( 0, " " );
buffer.insert( 0, floatValue );
buffer.insert( 0, " " );
buffer.insert( 0, doubleValue );
System.out.printf(
"buffer after inserts:\n%s\n\n", buffer.toString() );
System.out.printf(
"buffer after deletes:\n%s\n", buffer.toString() );
} // end main
} // end class StringBuilderInsertDelete