【java】StringBuffer方法

StringBufferText.java

String对象一经声明,便不能修改它的内容,修改的只是引用的地址;而StringBuffer对象是可以改变它的内容的

package text;
import java.io.BufferedReader;
import java.io.IOException;//异常处理
import java.io.InputStreamReader;
public class StringBufferText {	
    public static void main(String[] args) throws IOException {
    	BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    	String line;
    	StringBuffer sb1=new StringBuffer(48);
    	StringBuffer sb2=new StringBuffer("Hello World");
    	String s1="This is stringtext";
    	String s2=new String("That is stingtext"); 
    	int a5=s1.length();
    	System.out.println(a5);
        System.out.println(sb1);
        System.out.println(sb1.capacity());
        System.out.println(sb2);
        System.out.println(sb1.equals(sb2));//打印对比的结果,利用equal,来自object;
     //   System.out.println(sb1.tostring());1.写继承object在使用,2 自定义复写
        sb2.append("千夜");
        sb1.append("千夜");//为追加,若原未满则不上前面
        System.out.println(sb1);
        System.out.println(sb1.capacity());
        line=br.readLine();
        System.out.println(line);
        System.out.println(sb2);
        sb2.insert(13,"j");
        System.out.println(sb2);
        
    }
}

发布了27 篇原创文章 · 获赞 5 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_38304672/article/details/90083088