자바 StringBuffer 상세 설명
10694 단어 자바StringBuffer
다음은 여러분 에 게 실제 데모 작업 예 를 제공 합 니 다.
/**
* StringBuffer
* @author zhouguohui
*/
import java.util.HashMap;
public class StringBufferTest {
public static void main(String[] args) {
testInsertAPIs() ;
testAppendAPIs() ;
testReplaceAPIs() ;
testDeleteAPIs() ;
testIndexAPIs() ;
testOtherAPIs() ;
}
/**
* StringBuffer API
*/
private static void testOtherAPIs() {
System.out.println("-------------------------------- testOtherAPIs --------------------------------");
StringBuffer sbuilder = new StringBuffer("0123456789");
int cap = sbuilder.capacity();
System.out.printf("cap=%d
", cap);
char c = sbuilder.charAt(6);
System.out.printf("c=%c
", c);
char[] carr = new char[4];
sbuilder.getChars(3, 7, carr, 0);
for (int i=0; i<carr.length; i++)
System.out.printf("carr[%d]=%c ", i, carr[i]);
System.out.println();
System.out.println();
}
/**
* StringBuffer index API
*/
private static void testIndexAPIs() {
System.out.println("-------------------------------- testIndexAPIs --------------------------------");
StringBuffer sbuilder = new StringBuffer("abcAbcABCabCaBcAbCaBCabc");
System.out.printf("sbuilder=%s
", sbuilder);
// 1. , "bc"
System.out.printf("%-30s = %d
", "sbuilder.indexOf(\"bc\")", sbuilder.indexOf("bc"));
// 2. 5 , , "bc"
System.out.printf("%-30s = %d
", "sbuilder.indexOf(\"bc\", 5)", sbuilder.indexOf("bc", 5));
// 3. , "bc"
System.out.printf("%-30s = %d
", "sbuilder.lastIndexOf(\"bc\")", sbuilder.lastIndexOf("bc"));
// 4. 4 , , "bc"
System.out.printf("%-30s = %d
", "sbuilder.lastIndexOf(\"bc\", 4)", sbuilder.lastIndexOf("bc", 4));
System.out.println();
}
/**
* StringBuffer replace()
*/
private static void testReplaceAPIs() {
System.out.println("-------------------------------- testReplaceAPIs ------------------------------");
StringBuffer sbuilder;
sbuilder = new StringBuffer("0123456789");
sbuilder.replace(0, 3, "ABCDE");
System.out.printf("sbuilder=%s
", sbuilder);
sbuilder = new StringBuffer("0123456789");
sbuilder.reverse();
System.out.printf("sbuilder=%s
", sbuilder);
sbuilder = new StringBuffer("0123456789");
sbuilder.setCharAt(0, 'M');
System.out.printf("sbuilder=%s
", sbuilder);
System.out.println();
}
/**
* StringBuffer delete()
*/
private static void testDeleteAPIs() {
System.out.println("-------------------------------- testDeleteAPIs -------------------------------");
StringBuffer sbuilder = new StringBuffer("0123456789");
// 0 , “123456789”。
sbuilder.deleteCharAt(0);
// 3( ) 6( ) , “123789”。
sbuilder.delete(3,6);
// sb 1
String str1 = sbuilder.substring(1);
// sb 3( ) 5( )
String str2 = sbuilder.substring(3, 5);
// sb 3( ) 5( ) , CharSequence , String
String str3 = (String)sbuilder.subSequence(3, 5);
System.out.printf("sbuilder=%s
str1=%s
str2=%s
str3=%s
",
sbuilder, str1, str2, str3);
System.out.println();
}
/**
* StringBuffer insert()
*/
private static void testInsertAPIs() {
System.out.println("-------------------------------- testInsertAPIs -------------------------------");
StringBuffer sbuilder = new StringBuffer();
// 0
sbuilder.insert(0, new char[]{'a','b','c','d','e'});
// 0 。0 ,3
sbuilder.insert(0, new char[]{'A','B','C','D','E'}, 0, 3);
// 0 float
sbuilder.insert(0, 1.414f);
// 0 double
sbuilder.insert(0, 3.14159d);
// 0 boolean
sbuilder.insert(0, true);
// 0 char
sbuilder.insert(0, '
');
// 0 int
sbuilder.insert(0, 100);
// 0 long
sbuilder.insert(0, 12345L);
// 0 StringBuilder
sbuilder.insert(0, new StringBuffer("StringBuilder"));
// 0 StringBuilder 。6 0 ( ),13 ( )
sbuilder.insert(0, new StringBuffer("STRINGBUILDER"), 6, 13);
// 0 StringBuffer 。
sbuilder.insert(0, new StringBuffer("StringBuffer"));
// 0 StringBuffer 。6 0 ( ),12 ( )
sbuilder.insert(0, new StringBuffer("STRINGBUFFER"), 6, 12);
// 0 String 。
sbuilder.insert(0, "String");
// 0 String 。1 0 ( ),6 ( )
sbuilder.insert(0, "0123456789", 1, 6);
sbuilder.insert(0, '
');
// 0 Object 。 HashMap
HashMap map = new HashMap();
map.put("1", "one");
map.put("2", "two");
map.put("3", "three");
sbuilder.insert(0, map);
System.out.printf("%s
", sbuilder);
}
/**
* StringBuffer append()
*/
private static void testAppendAPIs() {
System.out.println("-------------------------------- testAppendAPIs -------------------------------");
StringBuffer sbuilder = new StringBuffer();
//
sbuilder.append(new char[]{'a','b','c','d','e'});
// 。0 ,3
sbuilder.append(new char[]{'A','B','C','D','E'}, 0, 3);
// float
sbuilder.append(1.414f);
// double
sbuilder.append(3.14159d);
// boolean
sbuilder.append(true);
// char
sbuilder.append('
');
// int
sbuilder.append(100);
// long
sbuilder.append(12345L);
// StringBuilder
sbuilder.append(new StringBuffer("StringBuilder"));
// StringBuilder 。6 ( ),13 ( )
sbuilder.append(new StringBuffer("STRINGBUILDER"), 6, 13);
// StringBuffer 。
sbuilder.append(new StringBuffer("StringBuffer"));
// StringBuffer 。6 ( ),12 ( )
sbuilder.append(new StringBuffer("STRINGBUFFER"), 6, 12);
// String 。
sbuilder.append("String");
// String 。1 ( ),6 ( )
sbuilder.append("0123456789", 1, 6);
sbuilder.append('
');
// Object 。 HashMap
HashMap map = new HashMap();
map.put("1", "one");
map.put("2", "two");
map.put("3", "three");
sbuilder.append(map);
sbuilder.append('
');
// unicode
sbuilder.appendCodePoint(0x5b57); // 0x5b57 “ ” unicode
sbuilder.appendCodePoint(0x7b26); // 0x7b26 “ ” unicode
sbuilder.appendCodePoint(0x7f16); // 0x7f16 “ ” unicode
sbuilder.appendCodePoint(0x7801); // 0x7801 “ ” unicode
System.out.printf("%s
", sbuilder);
}
}
실행 결 과 는:
-------------------------------- testInsertAPIs -------------------------------
{3=three, 2=two, 1=one}
12345StringBUFFERStringBufferBUILDERStringBuilder12345100
true3.141591.414ABCabcde
-------------------------------- testAppendAPIs -------------------------------
abcdeABC1.4143.14159true
10012345StringBuilderBUILDERStringBufferBUFFERString12345
{3=three, 2=two, 1=one}
-------------------------------- testReplaceAPIs ------------------------------
sbuilder=ABCDE3456789
sbuilder=9876543210
sbuilder=M123456789
-------------------------------- testDeleteAPIs -------------------------------
sbuilder=123789
str1=23789
str2=78
str3=78
-------------------------------- testIndexAPIs --------------------------------
sbuilder=abcAbcABCabCaBcAbCaBCabc
sbuilder.indexOf("bc") = 1
sbuilder.indexOf("bc", 5) = 22
sbuilder.lastIndexOf("bc") = 22
sbuilder.lastIndexOf("bc", 4) = 4
-------------------------------- testOtherAPIs --------------------------------
cap=26
c=6
carr[0]=3 carr[1]=4 carr[2]=5 carr[3]=6
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.