String是不变类,用String修改字符串会新建一个String对象,如果频繁的修改,将会产生很多的String对象,开销很大.因此java提供了一个StringBuffer类,这个类在修改字符串方面的效率比String高了很多。

在java中有3个类来负责字符的操作。

1.Character 是进行单个字符操作的,

2.String 对一串字符进行操作。不可变类。

3.StringBuffer 也是对一串字符进行操作,但是可变类。

  1. public class UsingStringBuffer {
  2. /**
  3. * 查找匹配字符串
  4. */
  5. public static void testFindStr() {
  6. StringBuffer sb = new StringBuffer();
  7. sb.append("This is a StringBuffer");
  8. // 返回子字符串在字符串中最先出现的位置,如果不存在,返回负数
  9. System.out.println("sb.indexOf(\"is\")=" + sb.indexOf("is"));
  10. // 给indexOf方法设置参数,指定匹配的起始位置
  11. System.out.println("sb.indexOf(\"is\")=" + sb.indexOf("is", 3));
  12. // 返回子字符串在字符串中最后出现的位置,如果不存在,返回负数
  13. System.out.println("sb.lastIndexOf(\"is\") = " + sb.lastIndexOf("is"));
  14. // 给lastIndexOf方法设置参数,指定匹配的结束位置
  15. System.out.println("sb.lastIndexOf(\"is\", 1) = "
  16. + sb.lastIndexOf("is", 1));
  17. }
  18. /**
  19. * 截取字符串
  20. */
  21. public static void testSubStr() {
  22. StringBuffer sb = new StringBuffer();
  23. sb.append("This is a StringBuffer");
  24. // 默认的终止位置为字符串的末尾
  25. System.out.print("sb.substring(4)=" + sb.substring(4));
  26. // substring方法截取字符串,可以指定截取的起始位置和终止位置
  27. System.out.print("sb.substring(4,9)=" + sb.substring(4, 9));
  28. }
  29. /**
  30. * 获取字符串中某个位置的字符
  31. */
  32. public static void testCharAtStr() {
  33. StringBuffer sb = new StringBuffer("This is a StringBuffer");
  34. System.out.println(sb.charAt(sb.length() - 1));
  35. }
  36. /**
  37. * 添加各种类型的数据到字符串的尾部
  38. */
  39. public static void testAppend() {
  40. StringBuffer sb = new StringBuffer("This is a StringBuffer!");
  41. sb.append(1.23f);
  42. System.out.println(sb.toString());
  43. }
  44. /**
  45. * 删除字符串中的数据
  46. */
  47. public static void testDelete() {
  48. StringBuffer sb = new StringBuffer("This is a StringBuffer!");
  49. sb.delete(0, 5);
  50. sb.deleteCharAt(sb.length() - 1);
  51. System.out.println(sb.toString());
  52. }
  53. /**
  54. * 向字符串中插入各种类型的数据
  55. */
  56. public static void testInsert() {
  57. StringBuffer sb = new StringBuffer("This is a StringBuffer!");
  58. // 能够在指定位置插入字符、字符数组、字符串以及各种数字和布尔值
  59. sb.insert(2, 'W');
  60. sb.insert(3, new char[] { 'A', 'B', 'C' });
  61. sb.insert(8, "abc");
  62. sb.insert(2, 3);
  63. sb.insert(3, 2.3f);
  64. sb.insert(6, 3.75d);
  65. sb.insert(5, 9843L);
  66. sb.insert(2, true);
  67. System.out.println("testInsert: " + sb.toString());
  68. }
  69. /**
  70. * 替换字符串中的某些字符
  71. */
  72. public static void testReplace() {
  73. StringBuffer sb = new StringBuffer("This is a StringBuffer!");
  74. // 将字符串中某段字符替换成另一个字符串
  75. sb.replace(10, sb.length(), "Integer");
  76. System.out.println("testReplace: " + sb.toString());
  77. }
  78. /**
  79. * 将字符串倒序
  80. */
  81. public static void reverseStr() {
  82. StringBuffer sb = new StringBuffer("This is a StringBuffer!");
  83. System.out.println(sb.reverse()); // reverse方法将字符串倒序
  84. }
  85. }

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

JAVA中StringBuffer类常用方法的更多相关文章

  1. JAVA中StringBuffer类常用方法详解

    String是不变类,用String修改字符串会新建一个String对象,如果频繁的修改,将会产生很多的String对象,开销很大.因此java提供了一个StringBuffer类,这个类在修改字符串 ...

  2. Java中StringBuffer类的常用方法

    StringBuffer:StringBuffer类型 描述:在实际应用中,经常回遇到对字符串进行动态修改.这时候,String类的功能受到限制,而StringBuffer类可以完成字符串的动态添加. ...

  3. JAVA中String类常用方法 I

    String类常用方法有: int length() -– 返回当前字符串的长度 int indexOf(int ch) -– 查找ch字符在该字符串中第一次出现的位置 int indexOf(Str ...

  4. Java中StringBuffer类

    StringBuffer: 线程安全的可变字符串. StringBuffer和String的区别?前者长度和内容可变,后者不可变.如果使用前者做字符串的拼接,不会浪费太多的资源. StringBuff ...

  5. Java中StringBuffer类append方法的使用

    public static void testAppend() { StringBuffer sb = new StringBuffer("This is a StringBuffer!&q ...

  6. Java中 ArrayList类常用方法和遍历

     ArrayList类对于元素的操作,基本体现在——增.删.查.常用的方法有: public boolean add(E e) :将指定的元素添加到此集合的尾部. public E remove(in ...

  7. Java中String类常用方法(字符串中的子字符串的个数)

    重点内容 4种方法: 1.int indexOf(String str)返回第一次出现的指定子字符串在此字符串中的索引. 2.int indexOf(String str, int startInde ...

  8. java中File类的常用方法总结

    java中File类的常用方法 创建: createNewFile()在指定的路径创建一个空文件,成功返回true,如果已经存在就不创建,然后返回false. mkdir() 在指定的位置创建一个此抽 ...

  9. 【转】JAVA的StringBuffer类

    [转]JAVA的StringBuffer类    StringBuffer类和String一样,也用来代表字符串,只是由于StringBuffer的内部实现方式和String不同,所以StringBu ...

随机推荐

  1. Button MouseEvent颜色变化

    public partial class Form1 : Form { public Form1() { InitializeComponent(); this.button1.Enter += bu ...

  2. java多线程有几种实现方法,都是什么?同步有几种实现方法,都是什么?

    多线程有两种实现方法,分别是继承Thread类与实现Runnable接口 同步的实现方面有两种,分别是synchronized,wait与notify 先看一下java线程运行时各个阶段的运行状态 j ...

  3. prologue epilogue

    https://www.hackerschool.com/blog/7-understanding-c-by-learning-assemblyhttps://www.hackerschool.com ...

  4. 控制textbook输入字符

    在KeyPress事件中假如如下代码此实例表示可输入数字退格和“.”. 具体字符KeyChar见连接 http://www.cnblogs.com/linji/archive/2012/10/24/2 ...

  5. 类似java.lang.NoClassDefFoundError: org/jaxen/JaxenException解决方法

    在使用dom4j的xpath时出现java.lang.NoClassDefFoundError: org/jaxen/JaxenException的异常,原因是dom4j引用了jaxen jar包,而 ...

  6. Extjs4 up 和down的用法

    Extjs4.x中,每个组件都新增加了两个方法up()和down()方法.这两个方法都是用来获取组件的,下面我们来看下up()方法和down()方法的官方解释. Extjs4.x中,新增加了两个方法u ...

  7. python------unicode字符串转换为其他类型

    问题描述: 一下字符串转换为json类型 {u'src': u'crawl', u'cid': u'Ctengbangguoji', u'datatype': u'ItemBase', u'times ...

  8. 十大滤波算法程序大全(Arduino精编无错版)(转)

    源:十大滤波算法程序大全(Arduino精编无错版) 转载请注明出处:极客工坊  http://www.geek-workshop.com/thread-7694-1-1.html

  9. bzoj3110: [Zjoi2013]K大数查询 【cdq分治&树套树】

    模板题,折腾了许久. cqd分治整体二分,感觉像是把询问分到答案上. #include <bits/stdc++.h> #define rep(i, a, b) for (int i = ...

  10. SD卡初始化以及命令详解

    SD卡是嵌入式设备中很常用的一种存储设备,体积小,容量大,通讯简单,电路简单所以受到很多设备厂商的欢迎,主要用来记录设备运行过程中的各种信息,以及程序的各种配置信息,很是方便,有这样几点是需要知道的 ...