您的位置:首页 > Web前端

java基础学习API之StringBuffer类 六-2

2017-02-22 21:56 405 查看
StringBuffer

字符串缓冲器类



package wsj02;
/**
*
* @author Angus
*	StringBuffer //字符串缓冲器类
*	与String的区别?
*		String一旦赋值就不能发生改变,但是StringBuffer可以
*	为什么?
*		StringBuffer采用的缓冲区机制
*		一开始先开辟一些空间,然后随着数据的增多,可以继续开辟空间,这些操作针对的同一个对象。
*	构造方法:
*		StringBuffer() : 构造一个其中不带字符的字符串缓冲区,初始容量为 16 个字符。
*		StringBuffer(int capacity) :构造一个不带字符,但具有指定初始容量的字符串缓冲区。
*		StringBuffer(String str) : 构造一个字符串缓冲区,并将其内容初始化为指定的字符串内容。
*
*		String 和StringBuffer可以通过构造方法互相转换通过上面的构造
*	成员方法:
*		length()
*		capacity() 	//字符容量,实际长度
*
*/
public class StringBufferDemo {
public static void main(String[] args) {
//方式1 StringBuffer()
StringBuffer sb = new StringBuffer();
System.out.println("sb"+sb);
System.out.println("sb.length()"+sb.length());
System.out.println("sb.capacity()"+sb.capacity());
System.out.println("---------------------------");

//方式2 StringBuffer(int capacity)
StringBuffer sb2 = new StringBuffer(50);
System.out.println("sb2"+sb2);
System.out.println("sb2.length()"+sb2.length());
System.out.println("sb2.capacity()"+sb2.capacity());
System.out.println("---------------------------");

//方式3  StringBuffer(String str)
StringBuffer sb3 = new StringBuffer("hello");
System.out.println("sb3"+sb3);
System.out.println("sb3.length()"+sb3.length());
System.out.println("sb3.capacity()"+sb3.capacity());
System.out.println("---------------------------");

}
}


构造方法



方法有很多



测试添加和删除功能:

package wsj02;
/**
*
* @author Angus
*	成员方法:
*		append()  //类型很多,基本都可以添加
*		insert(int offset, char c)	//插入方法从开始位置插入类型
*		delete(int start, int end) //删除开始到结束
deleteCharAt(int index)   //删除一个字段

*/
public class StringBufferDemo {
public static void main(String[] args) {
//方式1 增加
StringBuffer sb = new StringBuffer();
//		sb.append("123");
//		sb.append("asd");
//		sb.append(true);
//		sb.append(12.5);
System.out.println(sb);  //123asdtrue12.5
//简化
sb.append("123").append("asd").append(true).append(12.5); //因为返回为同一个对象
System.out.println(sb);  //123asdtrue12.5

//方式1 删除;
sb.delete(0, 3);
System.out.println(sb);  //asdtrue12.5   包左不包右
sb.deleteCharAt(5);
System.out.println(sb); //asdtre12.5
//删除所有元素
sb.delete(0, sb.length());
System.out.println(sb);
}
}


字符串反转:

package wsj02;

import java.util.Scanner;

/**
*
* @author Angus
*	实现字符串反转
*	方式一:
*		1:把字符串转换成数组
*		2;倒序输出
*	方式二;
*		用StringBuffer中的reverse() 实现
*/
public class StringBufferDemo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String line = sc.nextLine();
System.out.println("请输入字符串");
//方式一:
//		char[] ch = line.toCharArray();
//		for(int i = ch.length-1; i>= 0 ; i--){
//			System.out.print(ch[i]);
//		}
//方式二;
StringBuffer sb = new StringBuffer(line);
sb.reverse();
System.out.print(sb);

}
}
小技巧:

package wsj02;

import java.util.Scanner;

/**
*
* @author Angus
*	小面试
*/
public class StringBufferDemo {
public static void main(String[] args) {
String s = "abc";
StringBuffer s2 = new StringBuffer("abc");
add(s);
add2(s2);
System.out.println(s);//abc  字符串是一个特殊的引用类型
System.out.println(s2);//abchello  解决

}
public static void add2(StringBuffer s2) {
s2.append("hello");
System.out.println(s2);//abchello 解决
}

public static void add(String s) {
s += "hello";
System.out.println(s); //abchello
}

}

package wsj02;

/**
*
* @author Angus
*	常量相加会直接累加,然后再常量池里面去找有没有,如果有就是常量池的值
*/
public class StringBufferDemo {
public static void main(String[] args) {
String s1 = "a";
String s2 = "b";
String s3 = "ab";
System.out.println(s3 == s1 +s2);  //false
System.out.println(s3 == "a" +"b"); //true

}

}


最后附上JDK使用文档API 下载

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java StringBuffer