详解Java中StringBuffer类常用方法

所属分类: 软件编程 / java 阅读数: 51
收藏 0 赞 0 分享

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方法有三个参数,第一个参数指定被替换子串的起始位置,第二个参数指定被替换子串的终止位置,第三个参数指定新子串。

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

更多精彩内容其他人还在看

利用MultipartFile实现文件上传功能

这篇文章主要为大家详细介绍了利用MultipartFile实现文件上传功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Java编程实现NBA赛事接口调用实例代码

这篇文章主要介绍了Java编程实现NBA赛事接口调用实例代码,具有一定参考价值,需要的朋友可以了解下。
收藏 0 赞 0 分享

Java编程之双重循环打印图形

这篇文章主要介绍了Java编程之双重循环打印图形,属于Java编程基础练习部分,具有一定参考价值,需要的朋友可以了解下。
收藏 0 赞 0 分享

java基础学习JVM中GC的算法

这篇文章主要介绍了java基础学习JVM中GC的算法,通过图文加深对GC算法思路的理解。
收藏 0 赞 0 分享

Java编程Post数据请求和接收代码详解

这篇文章主要介绍了Java编程Post数据请求和接收代码详解,涉及enctype的三种编码,post与get等相关内容,具有一定参考价值,需要的朋友可以了解下。
收藏 0 赞 0 分享

Retrofit+Rxjava实现文件上传和下载功能

这篇文章主要介绍了Retrofit+Rxjava实现文件上传和下载功能,文中提到了单文件上传和多文件上传及相关参数的请求,需要的朋友参考下吧
收藏 0 赞 0 分享

Retrofit+Rxjava下载文件进度的实现

这篇文章主要介绍了Retrofit+Rxjava下载文件进度的实现,非常不错,具有参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

java检查服务器的连通两种方法代码分享

这篇文章主要介绍了java检查服务器的连通两种方法代码分享,涉及ping的介绍以及检查服务器连通的两种方法代码示例,具有一定参考价值,需要的朋友可以了解下。
收藏 0 赞 0 分享

Java/Android 获取网络重定向文件的真实URL的示例代码

本篇文章主要介绍了Java/Android 获取网络重定向文件的真实URL的示例代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

java并发编程之同步器代码示例

这篇文章主要介绍了java并发编程之同步器代码示例,分享了相关代码,具有一定参考价值,需要的朋友可以了解下。
收藏 0 赞 0 分享
查看更多