Java by API/java.lang/StringBuffer — различия между версиями

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

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

new StringBuffer(int size)

  
/*
 * Output:
   Length: 0
   Capacity:40
 * */
public class MainClass {
  public static void main(String args[]) {
    StringBuffer sb = new StringBuffer(40);
    System.out.println(sb);
    System.out.println("Length: "+sb.length());
    System.out.println("Capacity:"+sb.capacity());
  }
}





new StringBuffer(String str)

  
public class Main {
  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);
  }
}





StringBuffer: append(char c)

 

public class Main {
  public static void main(String[] args) {
    StringBuffer sb = new StringBuffer();
    sb.append(true);
    sb.append("A");
    char[] carray = { "a", "b", "c" };
    sb.append(carray);
    sb.append(carray, 0, 1);
    sb.append(3.5d);
    sb.append(2.4f);
    sb.append(45);
    sb.append(90000l);
    sb.append("That"s all!");
    System.out.println(sb);
  }
}





StringBuffer: append(CharSequence s, int start, int end)

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





StringBuffer: append(char[] str, int offset, int len)

  
public class Main {
  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);
  }
}





StringBuffer: append(double d)

  
public class Main {
  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);
  }
}





StringBuffer: append(float f)

 

public class Main {
  public static void main(String[] args) {
    StringBuffer sb = new StringBuffer();
    sb.append(true);
    sb.append("A");
    char[] carray = { "a", "b", "c" };
    sb.append(carray);
    sb.append(carray, 0, 1);
    sb.append(3.5d);
    sb.append(2.4f);
    sb.append(45);
    sb.append(90000l);
    sb.append("That"s all!");
    System.out.println(sb);
  }
}





StringBuffer: append(int intValue)

  
/*
 * Output:
 * 5
 * */
public class MainClass {
  public static void main(String args[]) {
  StringBuffer sb = new StringBuffer(40);
  sb.append(5);
  System.out.println(sb);
  }
}





StringBuffer: append(long lng)

  
public class Main {
  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);
  }
}





StringBuffer: append(String stringValue)

  
/*
 * Output:
 * a = 
 * */
public class MainClass {
  public static void main(String args[]) {
    StringBuffer sb = new StringBuffer(40);
    sb.append("a = ");
    System.out.println(sb);
  }
}





StringBuffer: capacity

  
/*
 * Output:
   capacity = 21
   
 * */
public class MainClass {
  public static void main(String args[]) {
  StringBuffer sb = new StringBuffer("Hello");
  System.out.println("capacity = " + sb.capacity());
  }
}





StringBuffer: charAt(int index)

  
/*
 * Output:
 * buffer before = Hello
   charAt(1) before = e
 * */
public class MainClass {
  public static void main(String args[]) {
  StringBuffer sb = new StringBuffer("Hello");
  System.out.println("buffer before = " + sb);
  System.out.println("charAt(1) before = " + sb.charAt(1));
  }
}





StringBuffer: deleteCharAt(int pos)

  
/*
 * Output:
After delete: This a test.
After deleteCharAt: his a test. 
 */
public class MainClass {
  public static void main(String args[]) {
    StringBuffer sb = new StringBuffer("This is a test.");
    sb.delete(4, 7);
    System.out.println("After delete: " + sb);
    sb.deleteCharAt(0);
    System.out.println("After deleteCharAt: " + sb);
  }
}





StringBuffer: delete(int start, int end)

  
/*
 * Output:
After delete: This a test.
After deleteCharAt: his a test. 
 */
public class MainClass {
  public static void main(String args[]) {
    StringBuffer sb = new StringBuffer("This is a test.");
    sb.delete(4, 7);
    System.out.println("After delete: " + sb);
    sb.deleteCharAt(0);
    System.out.println("After deleteCharAt: " + sb);
  }
}





StringBuffer: getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)

  
public class Main {
  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: ");
  }
}





StringBuffer: insert(int offset, boolean b)

  
public class Main {
  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());
  }
}





StringBuffer: insert(int offset, char c)

  
public class Main {
  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());
  }
}





StringBuffer: insert(int offset, char[] str)

  
public class Main {
  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());
  }
}





StringBuffer: insert(int offset, double d)

  
public class Main {
  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());
  }
}





StringBuffer: insert(int offset, float f)

  
public class Main {
  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());
  }
}





StringBuffer: insert(int offset, int i)

  
public class Main {
  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());
  }
}





StringBuffer: insert(int offset, long l)

  
public class Main {
  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());
  }
}





StringBuffer: insert(int offset, Object obj)

  
public class Main {
  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());
  }
}





StringBuffer: insert(int offset, String stringValue)

  
/*
 * Output:
 * hello there world!   
 * */
public class MainClass {
  public static void main(String args[]) {
  StringBuffer sb = new StringBuffer("hello world!");
  sb.insert(6, "there ");
  System.out.println(sb);
  }
}





StringBuffer: lastIndexOf(String str)

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





StringBuffer: length()

  
/*
 * Output:
   length = 5
   
 * */
public class MainClass {
  public static void main(String args[]) {
  StringBuffer sb = new StringBuffer("Hello");
  System.out.println("length = " + sb.length());
  }
}





StringBuffer: replace(int start, int end, String str)

  
/*
 * Output:
After replace: This was a test. 
 */
public class MainClass {
  public static void main(String args[]) {
    StringBuffer sb = new StringBuffer("This is a test.");
    sb.replace(5, 7, "was");
    System.out.println("After replace: " + sb);
  }
}





StringBuffer: reverse()

  
/*
 * Output:
jihgfedcbaedcba
jihgfedcbaedcba


 */

public class MainClass {
  public static void main(String args[]) {
    StringBuffer sb1 = new StringBuffer("abcde");
    sb1.append("abcdefghij");
    StringBuffer sb2 = sb1.reverse();
    System.out.println(sb1);
    System.out.println(sb2);
  }
}





StringBuffer: setCharAt(int index)

  
/*
 * Output:
 * buffer before = Hello
   charAt(1) before = e
   buffer after = Hillo
   charAt(1) after = i
   
 * */
public class MainClass {
  public static void main(String args[]) {
  StringBuffer sb = new StringBuffer("Hello");
  System.out.println("buffer before = " + sb);
  System.out.println("charAt(1) before = " + sb.charAt(1));
  sb.setCharAt(1, "i");
  System.out.println("buffer after = " + sb);
  System.out.println("charAt(1) after = " + sb.charAt(1));
  }
}





StringBuffer: setLength(int length)

  
/*
 * Output:
   buffer before = Hello
   charAt(1) before = e
   buffer after = He
   charAt(1) after = e
 * */
public class MainClass {
  public static void main(String args[]) {
  StringBuffer sb = new StringBuffer("Hello");
  System.out.println("buffer before = " + sb);
  System.out.println("charAt(1) before = " + sb.charAt(1));
  sb.setLength(2);
  System.out.println("buffer after = " + sb);
  System.out.println("charAt(1) after = " + sb.charAt(1));
  }
}





StringBuffer: toString()

  
/*
 * Output:
 * 5
 * */
public class MainClass {
  public static void main(String args[]) {
  StringBuffer sb = new StringBuffer(40);
  sb.append(5);
  String s = sb.toString();
  System.out.println(s);
  }
}