当前位置: 移动技术网 > IT编程>开发语言>Java > 详解Java中StringBuffer类常用方法

详解Java中StringBuffer类常用方法

2019年07月22日  | 移动技术网IT编程  | 我要评论

旺角十二金钗,恋熟吧,清平乐

string是不变类,用string修改字符串会新建一个string对象,如果频繁的修改,将会产生很多的string对象,开销很大.因此java提供了一个stringbuffer类,这个类在修改字符串方面的效率比string高了很多。
在java中有3个类来负责字符的操作。

  •   1.character 是进行单个字符操作的,
  •   2.string 对一串字符进行操作,不可变类。
  •   3.stringbuffer 也是对一串字符进行操作,但是可变类。
public class usingstringbuffer { 
  /** 
   * 查找匹配字符串 
   */ 
  public static void testfindstr() { 
    stringbuffer sb = new stringbuffer(); 
    sb.append("this is a stringbuffer"); 
    // 返回子字符串在字符串中最先出现的位置,如果不存在,返回负数 
    system.out.println("sb.indexof(\"is\")=" + sb.indexof("is")); 
    // 给indexof方法设置参数,指定匹配的起始位置 
    system.out.println("sb.indexof(\"is\")=" + sb.indexof("is", 3)); 
    // 返回子字符串在字符串中最后出现的位置,如果不存在,返回负数 
    system.out.println("sb.lastindexof(\"is\") = " + sb.lastindexof("is")); 
    // 给lastindexof方法设置参数,指定匹配的结束位置 
    system.out.println("sb.lastindexof(\"is\", 1) = " 
        + sb.lastindexof("is", 1)); 
  } 
 
  /** 
   * 截取字符串 
   */ 
  public static void testsubstr() { 
    stringbuffer sb = new stringbuffer(); 
    sb.append("this is a stringbuffer"); 
    // 默认的终止位置为字符串的末尾 
    system.out.print("sb.substring(4)=" + sb.substring(4)); 
    // substring方法截取字符串,可以指定截取的起始位置和终止位置 
    system.out.print("sb.substring(4,9)=" + sb.substring(4, 9)); 
  } 
 
  /** 
   * 获取字符串中某个位置的字符 
   */ 
  public static void testcharatstr() { 
    stringbuffer sb = new stringbuffer("this is a stringbuffer"); 
    system.out.println(sb.charat(sb.length() - 1)); 
  } 
 
  /** 
   * 添加各种类型的数据到字符串的尾部 
   */ 
  public static void testappend() { 
    stringbuffer sb = new stringbuffer("this is a stringbuffer!"); 
    sb.append(1.23f); 
    system.out.println(sb.tostring()); 
  } 
 
  /** 
   * 删除字符串中的数据 
   */ 
  public static void testdelete() { 
    stringbuffer sb = new stringbuffer("this is a stringbuffer!"); 
    sb.delete(0, 5); 
    sb.deletecharat(sb.length() - 1); 
    system.out.println(sb.tostring()); 
  } 
 
  /** 
   * 向字符串中插入各种类型的数据 
   */ 
  public static void testinsert() { 
    stringbuffer sb = new stringbuffer("this is a stringbuffer!"); 
    // 能够在指定位置插入字符、字符数组、字符串以及各种数字和布尔值 
    sb.insert(2, 'w'); 
    sb.insert(3, new char[] { 'a', 'b', 'c' }); 
    sb.insert(8, "abc"); 
    sb.insert(2, 3); 
    sb.insert(3, 2.3f); 
    sb.insert(6, 3.75d); 
    sb.insert(5, 9843l); 
    sb.insert(2, true); 
    system.out.println("testinsert: " + sb.tostring()); 
  } 
 
  /** 
   * 替换字符串中的某些字符 
   */ 
  public static void testreplace() { 
    stringbuffer sb = new stringbuffer("this is a stringbuffer!"); 
    // 将字符串中某段字符替换成另一个字符串 
    sb.replace(10, sb.length(), "integer"); 
    system.out.println("testreplace: " + sb.tostring()); 
  } 
 
  /** 
   * 将字符串倒序 
   */ 
  public static void reversestr() { 
    stringbuffer sb = new stringbuffer("this is a stringbuffer!"); 
    system.out.println(sb.reverse()); // reverse方法将字符串倒序 
  } 
} 

小结:
stringbuffer不是不变类,在修改字符串内容时,不会创建新的对象,因此,它比string类更适合修改字符串;
stringbuffer类没有提供同string一样的tochararray方法;
stringbuffer类的replace方法同string类的replace方法不同,它的replace方法有三个参数,第一个参数指定被替换子串的起始位置,第二个参数指定被替换子串的终止位置,第三个参数指定新子串。

以上就是本文的全部内容,希望对大家的学习有所帮助。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网