博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
javase个人垃圾复习笔记05Java StringBuffer 和 StringBuilder 类
阅读量:3968 次
发布时间:2019-05-24

本文共 2925 字,大约阅读时间需要 9 分钟。

Java StringBuffer 和 StringBuilder 类

当对字符串进行修改的时候,需要使用 StringBuffer 和 StringBuilder 类。

和 String 类不同的是,StringBuffer 和 StringBuilder 类的对象能够被多次的修改,并且不产生新的未使用对象。

StringBuilder 类在 Java 5 中被提出,它和 StringBuffer 之间的最大不同在于 StringBuilder 的方法不是线程安全的(不能同步访问)。

由于 StringBuilder 相较于 StringBuffer 有速度优势,所以多数情况下建议使用 StringBuilder 类。然而在应用程序要求线程安全的情况下,则必须使用 StringBuffer 类。

StringBuffer 方法

public StringBuffer append(String s)
将指定的字符串追加到此字符序列。

public class append {
public static void main(String []args){
StringBuffer sb1 = new StringBuffer("gao ze wei zhen shuai."); StringBuffer sb2 = new StringBuffer("gao ze wei zhen shuai."); StringBuffer sb3 = new StringBuffer("gaozewei+"); String a = "gao ze wei"; String b = "zhen shuai."; String c = ""; String d = "gzwgehsrtsh"; sb1.append(sb2); c = a + b; sb3.append(d, 3, 9); System.out.println(sb1); System.out.println(c); System.out.println(sb3); }}

public StringBuffer reverse()

将此字符序列用其反转形式取代。

public delete(int start, int end)

移除此序列的子字符串中的字符。

public class Test {
public static void main(String args[]) {
StringBuffer sb = new StringBuffer("Welcom to yiibai.com"); sb.delete(3, 7); System.out.println(sb); } }Java执行上面示例代码,得到以下结果:Welto yiibai.com

public insert(int offset, int i)

将 int 参数的字符串表示形式插入此序列中。

public class StringBufferDemo {
public static void main(String[] args) {
StringBuffer buff = new StringBuffer("zyxwvut"); System.out.println("buffer = " + buff); buff.insert(2, 989); System.out.print("After insertion = "); System.out.println(buff.toString()); } }/* 让我们来编译和运行上面的程序,这将产生以下结果:buffer = zyxwvutAfter insertion = zy989xwvut

replace(int start, int end, String str)

使用给定 String 中的字符替换此序列的子字符串中的字符。

String str3="abcd"; String str4=str3.replace("ab","ff"); System.out.println(str4); 输出结果为: ffcd StringBuffer str1=new StringBuffer("abcde"); StringBuffer str2=str1.replace(0,1,"ff"); System.out.println(str2); 输出结果为: ffbcde

下面的列表里的方法和 String 类的方法类似:

1 int capacity()
返回当前容量。
2 char charAt(int index)
返回此序列中指定索引处的 char 值。
3 void ensureCapacity(int minimumCapacity)
确保容量至少等于指定的最小值。
4 void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
将字符从此序列复制到目标字符数组 dst。
5 int indexOf(String str)
返回第一次出现的指定子字符串在该字符串中的索引。
6 int indexOf(String str, int fromIndex)
从指定的索引处开始,返回第一次出现的指定子字符串在该字符串中的索引。
7 int lastIndexOf(String str)
返回最右边出现的指定子字符串在此字符串中的索引。
8 int lastIndexOf(String str, int fromIndex)
返回 String 对象中子字符串最后出现的位置。
9 int length()
返回长度(字符数)。
10 void setCharAt(int index, char ch)
将给定索引处的字符设置为 ch。
11 void setLength(int newLength)
设置字符序列的长度。
12 CharSequence subSequence(int start, int end)
返回一个新的字符序列,该字符序列是此序列的子序列。
13 String substring(int start)
返回一个新的 String,它包含此字符序列当前所包含的字符子序列。
14 String substring(int start, int end)
返回一个新的 String,它包含此序列当前所包含的字符子序列。
15 String toString()
返回此序列中数据的字符串表示形式。

转载地址:http://utcki.baihongyu.com/

你可能感兴趣的文章
android开发环境搭建(for 驱动开发…
查看>>
android开发环境搭建(for 驱动开发…
查看>>
android驱动例子(LED灯控制)
查看>>
为Android内核添加新驱动,并添加…
查看>>
Android Permission中英对照
查看>>
Android Permission中英对照
查看>>
[转]Android下编译自己的库文件jar…
查看>>
[转]Android下编译自己的库文件jar…
查看>>
使用Android隐藏类和隐藏方法
查看>>
使用Android隐藏类和隐藏方法
查看>>
使用Android隐藏类和隐藏方法
查看>>
使用Android隐藏类和隐藏方法
查看>>
Android生成和引用第三方库(jar)的…
查看>>
Android生成和引用第三方库(jar)的…
查看>>
[转]Android下编译自己的库文件jar…
查看>>
[转]Android下编译自己的库文件jar…
查看>>
no signatures that match those i…
查看>>
no signatures that match those i…
查看>>
android签名INSTALL_FAILED_UPDATE…
查看>>
android签名INSTALL_FAILED_UPDATE…
查看>>