String Buffer和StringBuilder类-java

Java String Buffer和StringBuilder类

对字符串进行修改时,需要String Buffer和StringBuilder类

  • 能够被多次修改,却不产生新的未使用对象
  • StringBuilder类,不是线程安全的。
  • 速度优势,要求安全的情况下必须用StringBuffer类

    public class Test{
      public static void main(String args[]){ StringBuffer sBuffer = new StringBuffer("everyone:"); sBuffer.append("say: "); sBuffer.append("Hello,"); sBuffer.append("world"); System.out.println(sBuffer); } } 

  • StringBuffer方法

StringBuffer方法

编号 方法
1 public StringBuffer appen(String s)
将指定字符串追加到此字符串序列
2 public StringBuffer reverse()
反转字符串
3 public StringBuffer delete(int start,int end)
删除其中部分字符串
4 public StringBuffer insert(int offset,int i)
将int 参数的字符串表示形式插入到此序列中
5 replace(int start,int end, String str)
用指定的字符串替换此字符串中的字符

序号 方法
1 int capacity()
返回当前容量
2 char charAt(int index)
返回此序列中指定索引处的char值
3 void ensureCapacity(int minimumCapacity)
确保容量至少等于指定的最小值
4 void ensureCapacity(int secBegin,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 nt 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()
返回此序列中数据的字符串表示形式

猜你喜欢

转载自www.cnblogs.com/bomily0212/p/12082975.html
今日推荐