zoukankan      html  css  js  c++  java
  • StringBuffer

    StringBuffer类

    线程安全的编程字符序列

    StringBuffer和String的区别
    • String 是一个不可变的字符序列
    • StringBuffer是一个可变的字符序列
    StringBuffer的构造方法
    • public StringBuffer( ) : 无参构造方法
    • public StringBuffer(int capacity) : 指定容量的字符串缓冲区对象
    • public StringBuffer(String str) : 指定字符串内容缓冲区对象
    实例
    package com.mephisto.stringbuffer;
    
    public class DemoStringBuffer1 {
    	
    	public static void main(String[] args) {
    		StringBuffer stringBuffer = new StringBuffer();
    		System.out.println(stringBuffer.length());		// 容器的字符串个数, 实际值
    		System.out.println(stringBuffer.capacity());	// 容器的初始容量, 理论值
    		
    		
    		StringBuffer stringBuffer2 = new StringBuffer(10);
    		System.out.println(stringBuffer2.length());
    		System.out.println(stringBuffer2.capacity());
    
    		StringBuffer stringBuffer3 = new StringBuffer("Mephisto");
    		System.out.println(stringBuffer3.length());
    		System.out.println(stringBuffer3.capacity());
    		
    	}
    }
    
    
    输出结果
    0
    16
    0
    10
    8
    24
    
    StringBuffer的方法
    • public int capacity( ) : 返回当前容量 理论值
    • public int length( ) : 返回长度(字符数 ) 实际值
    StringBuffer的添加功能
    • public StringBuffer append(String str):
      • 可以把任意类型数据添加到字符串缓冲区里面,并返回缓冲区本身
    • public StringBuffer insert(int offset, String str):
      • 在指定位置吧任意类型的数据插入到字符串缓冲区里面, 并返回字符串缓冲区本身
    实例
    package com.mephisto.stringbuffer;
    
    public class DemoStringBuffer2 {
    
    	public static void main(String[] args) {
    		// demo1();
    		
    		StringBuffer sBuffer = new StringBuffer("1234");
    		// 在指定位置添加, 如果没有指定位置的索引就会报索引越界异常
    		sBuffer.insert(3, "Mephisto");	
    		System.out.println(sBuffer);
    	}
    
    	private static void demo1() {
    		/*
    		 * StringBuffer是字符串缓冲区, 当new的时候是在堆内存创建了一个对象,底层是一个长度为16的字符串数组
    		 * 当调用添加方法时, 不会再重新船舰对象,在不断向原缓冲区添加字符
    		 */
    		StringBuffer sBuffer = new StringBuffer();
    		StringBuffer sBuffer2 = sBuffer.append(true);
    		StringBuffer sBuffer3 = sBuffer.append("mephisto");
    		StringBuffer sBuffer4 = sBuffer.append(10);
    		
    		/* 四个引用指向同一个对象 */
    		System.out.println(sBuffer.toString());	
    		System.out.println(sBuffer2.toString());
    		System.out.println(sBuffer3.toString());
    		System.out.println(sBuffer4.toString());
    	}
    }
    
    
    StringBuffer的删除功能
    • public StringBuffer deleteCharAt(int index):
      • 删除指定位置的字符, 并返回本身
    • public StringBuffer delete(int start, int end):
      • 删除从指定位置开始指定位置结束的内容, 并返回本身
    实例
    package com.mephisto.stringbuffer;
    
    public class DemoStringBuffer3 {
    
    	public static void main(String[] args) {
    		StringBuffer sBuffer = new StringBuffer();
    		sBuffer.append("mephisto");
    		sBuffer.deleteCharAt(1);
    		
    		System.out.println(sBuffer);
    		
    		
    		// 删除的时候时包含头部,不包含尾部  0<=index<2, [0,2)
    		sBuffer.delete(0, 2);
    		System.out.println(sBuffer);
    		
    		//清空缓冲区
    		sBuffer.delete(0, sBuffer.length());
    		
    		// sBuffer = new StringBuffer();  不要用这种方式清空缓冲区, 这样是创建新的缓冲区, 原来的变成了垃圾,浪费内存
    		System.out.println("清空后: " + sBuffer);
    	}
    
    }
    
    输出结果
    mphisto
    histo
    清空后: 
    
    StringBuffer的替换和反转功能
    • StringBuffer替换功能
      • public StringBuffer repalce( int start, int end, String int ):
        • 从start 开始到 end 用 str 替换
    • StringBuffer的反转功能
      • public StringBuffer reverse( ):
        • 字符串反转
    实例
    package com.mephisto.stringbuffer;
    
    public class DemoStringBuffer4 {
    
    		public static void main(String[] args) {
    			StringBuffer sb = new StringBuffer("mephisto");
    			sb.replace(4, sb.length(), "1st0");
    			System.out.println(sb);
    			System.out.println(sb.reverse());
    		}
    }
    
    输出结果
    meph1st0
    0ts1hpem
    
    StringBuffer的截取功能
    • public String substring(int start):
      • 从制定位置街区到末尾
    • public String substring(int start, int end):
      • 截取从指定位置开始到结束位置,包括开始位置,不包括结束位置

    注意 : 返回值类型不再是StringBuffer本身

    实例
    package com.mephisto.stringbuffer;
    
    public class DemoStringBuffer {
    
    	public static void main(String[] args) {
    		int num = 1;
    		StringBuffer sBuffer = new StringBuffer("I like China!");
    		String string  = sBuffer.substring(4);
    		String string2 = sBuffer.substring(1,4);
    		
    		System.out.println(getType(num));
    		
    		System.out.println(getType(string));
    		
    		System.out.println(getType(string2));
    		
    		System.out.println(getType(sBuffer));
    	}
    	private static String getType(Object o) {
    		
    		return o.getClass().toString();
    	
    	}
    
    }
    
    输出结果
    class java.lang.Integer
    class java.lang.String
    class java.lang.String
    class java.lang.StringBuffer
    
    String --> StringBuffer转换
    • 通过构造方法
    • 通过append()方法
    StringBuffer --> String转换
    • 通过构造方法
    • 通过toString()方法
    • 通过subString(0,length);
    package com.mephisto.stringbuffer;
    
    public class DemoStringBuffer5 {
    	
    	public static void main(String[] args) {
    		
    		/* 通过构造方法  String --> StringBuffer */
    		StringBuffer sb1 = new StringBuffer("Mephisto");
    		System.out.println(sb1);
    		
    		
    		/* 通过append()方法   String --> StringBuffer */
    		
    		StringBuffer sb2 = new StringBuffer();
    		sb2.append("Mephisto");
    		System.out.println(sb2);
    		
    		
    		//	通过构造方法     StringBuffer --> String
    		StringBuffer sb3 = new StringBuffer("Mephisto");
    		
    		String string1 = new String(sb3);
    		System.out.println(string1);
    		
    		// 通过toString()方法  StringBuffer --> String
    		StringBuffer sb4 = new StringBuffer("Mephisto");
    		String string2 = sb4.toString();
    		System.out.println(string2);	
    		
    		// 通过subString(0,length);   StringBuffer --> String
    		StringBuffer sb5 = new StringBuffer("Mephisto");
    		String string3 = sb5.substring(0,sb5.length());
    		System.out.println(string3);
    	}
    
    }
    
    StringBuilder和StringBuffer区别

    StringBuilder是jdk1.0版本的, 是线程安全的, 效率低.

    StringBuffer是jdk1.5版本的, 是线程不安全的, 效率高.

    String 和 StringBuffer, StringBuilder的区别

    String是一个不可变的字符串序列

    StringBuffer, StringBuilder是可以变化字符序列

    形参问题
    • String作为参数传递
    • StringBuffer作为参数传递
    实例
    package com.mephisto.stringbuffer;
    
    public class DemoStringBuffer6 {
    
    	/*
    	 * String 作为参数传递
    	 * StringBuffer 作为参数传递
    	 * 
    	 * 基本数据类型的值传递, 不改变其值
    	 * 引用数据类型的值传递, 改变其值 
    	 * 
    	 * String类虽然是引用数据类型, 但是当作数据类型传递时和基本数据类型一样的
    	 * 
    	 * 
    	 */
    	
    	public static void main(String[] args) {
    
    		String string = "Mephisto";
    		System.out.println(string);
    		change(string);
    		System.out.println(string);
    		
    		StringBuffer sb = new StringBuffer();
    		sb.append("Mephisto");
    		System.out.println(sb);
    		change(sb);
    		System.out.println(sb);
    	}
    
    	public static void change(String string) {
    		string += " Lee";
    	}
    	public static void change(StringBuffer sb) {
    		sb.append(" Lee");
    	}
    
    }
    
    输出结果
    Mephisto
    Mephisto
    Mephisto
    Mephisto Lee
    
  • 相关阅读:
    github的使用
    QPalette的用法
    QTimer的用法
    QStatusBar的用法
    QWhatsThis的用法
    QString::​arg的用法
    qt中ui的 使用介绍
    安全协议IPSEC
    安全协议ssl
    对称加密和非对称加密
  • 原文地址:https://www.cnblogs.com/mephisto03/p/9474634.html
Copyright © 2011-2022 走看看