Java Tutorial/Development/StringBuffer StringBuilder — различия между версиями

Материал из Java эксперт
Перейти к: навигация, поиск
м (1 версия)
 
м (1 версия)
 
(нет различий)

Текущая версия на 15:30, 31 мая 2010

Содержание

Adding String to a StringBuffer Object

public class MainClass {
  public static void main(String[] arg) {
    StringBuffer newString = new StringBuffer("abcde1234567890");
 
    newString.append("saves nine");
    
    System.out.println(newString);
    
   }
}



abcde1234567890saves nine


Appending a subset of the elements from a char array

public class MainClass {
  public static void main(String[] arg) {
    StringBuffer buf = new StringBuffer("::");
    char[] text = { "i", "s", " ", "e", "x", "a", "c", "t", "l", "y"};
    buf.append(text, 2, 8);
    System.out.println(buf);
  }
}



:: exactly


Appending a Substring: append part of the aString object to the buf object

public class MainClass {
  public static void main(String[] arg) {
    StringBuffer buf = new StringBuffer("1234567890");
    String aString = "abcdefghijk";
    buf.append(aString, 3, 4);
    
    System.out.println(buf);
  }
}



1234567890d


Appending Basic Types: long, double

public class MainClass {
  public static void main(String[] arg) {
    StringBuffer buf = new StringBuffer("The number is ");
    long number = 999;
    buf.append(number);
    buf.append(12.34);
    System.out.println(buf);
  }
}



The number is 99912.34


Calling append() method to construct a StringBuffer object

public class MainClass {
  public static void main(String[] arg) {
    StringBuffer proverb = new StringBuffer(); // Capacity is 16
    proverb.append("A").append("B").append("C").append("D").append("E");
    System.out.println(proverb);
  }
}



ABCDE


Changing a single character in a StringBuffer object

public class MainClass {
  public static void main(String[] arg) {
    StringBuffer phrase = new StringBuffer("one two three four");
    phrase.setCharAt(3, "Z");
    System.out.println(phrase);
  }
}



oneZtwo three four


Changing the StringBufer Length for a StringBuffer Object

public class MainClass {
  public static void main(String[] arg) {
    StringBuffer newString = new StringBuffer("abcde1234567890");
    
    System.out.println(newString.capacity());
    System.out.println(newString.length());
    System.out.println(newString);
    
    newString.setLength(8);
    System.out.println(newString.capacity());
    System.out.println(newString.length());
    System.out.println(newString);
    
  }
}



31
15
abcde1234567890
31
8
abcde123


Check the capacity of StringBuilder object

public class Main {
  public static void main(String[] a) {
    StringBuilder builder1 = new StringBuilder();
    StringBuilder builder2 = new StringBuilder(0);
    StringBuilder builder3 = new StringBuilder(100);
    System.out.println(builder1.capacity());
    System.out.println(builder2.capacity());
    System.out.println(builder3.capacity());
  }
}
/*
16
0
100
*/





Convert digit to character with Character.forDigit

public class MainClass {
  public static void main(String args[]) {
    System.out.printf("Convert digit to character: %s\n", Character.forDigit(12, 2));
  }
}





Creating a StringBuffer object with a specific value for the capacity

public class MainClass {
  public static void main(String[] arg) {
    StringBuffer newString = new StringBuffer(50);
    
    System.out.println(newString.capacity());
  }
}



50


Creating a String Object From a StringBuffer Object

public class MainClass {
  public static void main(String[] arg) {
    StringBuffer palindrome = new StringBuffer("so many dynamos");

    String aString = palindrome.toString();
    System.out.println(aString);
  }
}



so many dynamos


Deletes text from the StringBuilder object

public class Main {
  public static void main(String[] a) {
    StringBuilder builder = new StringBuilder("Line 1\n");
    builder.append("Line 3\n");
    builder.insert(0, "Line 2\n");
    System.out.println(builder.toString());
    builder = builder.delete(2, 6);
    System.out.println(builder.toString());
  }
}
/*
Line 2
Line 1
Line 3
Li
Line 1
Line 3
*/





Extracting Characters From a Mutable String: charAt() and getChars() methods

public class MainClass {
  public static void main(String[] arg) {
    StringBuffer phrase = new StringBuffer("one two three four");
    
    System.out.println(phrase.charAt(5));
   
    char[] textArray = new char[3];
    phrase.getChars(9, 12, textArray, 0);
    for(char ch: textArray){
      System.out.println(ch);
      
    }
  }
}



w
h
r
e


Finding the Position of a Substring

public class MainClass {
  public static void main(String[] arg) {
    StringBuffer phrase = new StringBuffer("one two three four");
    int position = phrase.lastIndexOf("three");
    System.out.println(position);
  }
}



8


Inserting Strings

public class MainClass {
  public static void main(String[] arg) {
    StringBuffer phrase = new StringBuffer("one two three four");
    phrase.insert(4, "old");
    
    System.out.println(phrase);
    
  }
}



one oldtwo three four


Remove substring from StringBuilder

public class Main {
  public static void main(String[] args) {
    StringBuilder lipsum = new StringBuilder("Lorem ipsum dolor sit amet.");
    System.out.println("lipsum = " + lipsum.toString());
    lipsum.delete(0, 28);
    System.out.println("lipsum = " + lipsum.toString());
    lipsum.deleteCharAt(lipsum.length() - 1);
    System.out.println("lipsum = " + lipsum.toString());
  }
}





Removing several characters from a StringBuffer object

public class MainClass {
  public static void main(String[] arg) {
    StringBuffer phrase = new StringBuffer("one two three four");
    phrase.delete(5, 9);      
    System.out.println(phrase);
  }
}



one three four


Replacing a Substring in the Buffer

public class MainClass {
  public static void main(String[] arg) {
    StringBuffer phrase = new StringBuffer("one two three four");
    String substring = "two";
    String replacement = "twenty";
    int position = phrase.lastIndexOf(substring); // Find start of "two"
    phrase.replace(position, position + substring.length(), replacement);
    
    System.out.println(phrase);
  }
}



one twenty three four


Reversing the sequence of characters in a StringBuffer object with the reverse() method

public class MainClass {
  public static void main(String[] arg) {
    StringBuffer palindrome = new StringBuffer("so many dynamos");

    palindrome.reverse();
    System.out.println(palindrome);
  }
}



somanyd ynam os


Specifying the index position in the buffer where the search is to start

public class MainClass {
  public static void main(String[] arg) {
    StringBuffer phrase = new StringBuffer("one two three four");
    int position = phrase.lastIndexOf("three", 6);
    System.out.println(position);
  }
}



-1


StringBuffer and StringBuilder

  1. Using StringBuffer and StringBuilder to transform strings frequently: adding, deleting, or replacing substrings in a string.
  2. Operations will be faster and easier using mutable objects.
  3. All the operations in the context of StringBuffer are available with the StringBuilder class, which will be faster but not thread-safe.


StringBuffer methods charAt, setCharAt, getChars and reverse

public class MainClass
{
   public static void main( String args[] )
   {
      StringBuffer buffer = new StringBuffer( "hello there" );
      System.out.printf( "buffer = %s\n", buffer.toString() );
       System.out.printf( "Character at 0: %s\nCharacter at 4: %s\n\n", 
         buffer.charAt( 0 ), buffer.charAt( 4 ) );
      char charArray[] = new char[ buffer.length() ];
      buffer.getChars( 0, buffer.length(), charArray, 0 );
      System.out.print( "The characters are: " );
      for ( char character : charArray )
         System.out.print( character );
      buffer.setCharAt( 0, "H" );
      buffer.setCharAt( 6, "T" );
      System.out.printf( "\n\nbuf = %s", buffer.toString() );
      buffer.reverse();
      System.out.printf( "\n\nbuf = %s\n", buffer.toString() );
   }
}





StringBuffer methods insert, delete and deleteCharAt

public class MainClass {
  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;
    double doubleValue = 33.3;
    StringBuffer buffer = new StringBuffer();
    buffer.insert(0, objectRef);
    buffer.insert(0, "  ");
    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());
    buffer.deleteCharAt(10);
    buffer.delete(2, 6);
    System.out.printf("buffer after deletes:\n%s\n", buffer.toString());
  }
}



buffer after inserts:
33.3  2.5  10000000  7  K  true  def  abcdef  goodbye  hello
buffer after deletes:
332.5 10000000  7  K  true  def  abcdef  goodbye  hello


The append() method returns a reference to the extended StringBuffer object

public class MainClass {
  public static void main(String[] arg) {
    StringBuffer newString = new StringBuffer("abcde1234567890");
 
    StringBuffer newBuffer = newString.append("saves nine");
    
    System.out.println(newBuffer);
    
   }
}



abcde1234567890saves nine


The ensureCapacity() method changes the default capacity of a StringBuffer object

public class MainClass {
  public static void main(String[] arg) {
    StringBuffer newString = new StringBuffer("abcde");
    
    System.out.println(newString.capacity());
    
    newString.ensureCapacity(40);
    
    System.out.println(newString.capacity());
  }
}



/*
21
44


The Length of a StringBuffer Object

public class MainClass {
  public static void main(String[] arg) {
    StringBuffer aString = new StringBuffer("ABCDE");
    int length = aString.length();    
    System.out.println(length);
  }
}



5


To find out a StringBuffer object"s capacity

public class MainClass {
  public static void main(String[] arg) {
    StringBuffer newString = new StringBuffer("abcde");
    
    System.out.println(newString.capacity());
  }
}



21


Using the deleteCharAt() method to remove a character from a StringBuffer object at the index position

public class MainClass {
  public static void main(String[] arg) {
    StringBuffer phrase = new StringBuffer("one two three four");
    phrase.deleteCharAt(10);      
    System.out.println(phrase);
  }
}



one two thee four


You can also create a StringBuffer object using a reference stored inariable of type String:

public class MainClass {
  public static void main(String[] arg) {
    StringBuffer aString = new StringBuffer("ABCDE");

    String phrase = "abced";
    StringBuffer buffer = new StringBuffer(phrase);
    System.out.println(aString);
    System.out.println(buffer);
  }
}



ABCDE
abced