黑马程序员技术交流社区

标题: StringBuffer [打印本页]

作者: itheima_llt    时间: 2015-4-12 12:47
标题: StringBuffer
本帖最后由 itheima_llt 于 2015-4-12 15:18 编辑

想发一个StringBuffer的练习,怎么不让发了?见下面那张截图!
  1. class StringBufferDemo
  2. {
  3.         public static void main(String[] args)
  4.         {
  5.                 //add();
  6.                 //delete();
  7.                 update();
  8.         }

  9.         //1 存储
  10.         public static void add()
  11.         {
  12.                 StringBuffer sb = new StringBuffer();
  13.                 StringBuffer sb1 = sb.append("abcdefg");
  14.                 System.out.println(sb.toString());
  15.                 System.out.println(sb==sb1);//true

  16.                 sb.insert(1,"23");
  17.                 System.out.println(sb.toString());
  18.         }

  19.         //2 删除
  20.         public static void delete()
  21.         {
  22.                 StringBuffer sb = new StringBuffer("abcd123efgh");
  23.                 sb.delete(4,7);
  24.                 System.out.println(sb.toString());//结果abcdefgh.删除包含头不包含尾

  25.                 sb.deleteCharAt(7);
  26.                 System.out.println(sb.toString());//结果abcdefg

  27.                 sb.delete(0,sb.length());//sb被清空了,结果为空串
  28.         }

  29.         //3 修改
  30.         public static void update()
  31.         {
  32.                 StringBuffer sb = new StringBuffer("abcdefgh");
  33.                 sb.replace(0,3,"Hello java");
  34.                 System.out.println(sb.toString());//结果Hello javadefgh

  35.                 sb.setCharAt(0,'h');
  36.                 System.out.println(sb.toString());//结果hello javadefgh

  37.                 //4 反转
  38.                 sb.reverse();
  39.                 System.out.println(sb.toString());//结果hgfedavaj olleh

  40.                 //5 将缓冲区中指定数据存储到指定字符数组的指定位置中
  41.                 char[] ch = new char[7];
  42.                 sb.getChars(0,5,ch,0);
  43.                 System.out.println(new String(ch));//运行结果hgfed
  44.         }
  45. }
复制代码



为什么.jpg (75.13 KB, 下载次数: 1)

为什么.jpg

作者: itheima_llt    时间: 2015-4-12 21:50
为什么我会被拦截呢??




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2