学习笔记,仅供参考


文章目录

  • StringBuffer类
    • StringBuffer的替换和反转功能
      • replace方法
      • reverse方法
    • StringBuffer的截取功能
      • substring方法
    • 举几个例子

StringBuffer类

StringBuffer的替换和反转功能

replace方法

public StringBuffer replace(int start,int end,String str)

Replaces the characters in a substring of this sequence with characters in the specified String. The substring begins at the specified start and extends to the character at index end - 1 or to the end of the sequence if no such character exists. First the characters in the substring are removed and then the specified String is inserted at start. (This sequence will be lengthened to accommodate the specified String if necessary.)

  • 参数

    • start - The beginning index, inclusive.
    • end - The ending index, exclusive.
    • str - String that will replace previous contents.
  • 返回

    • This object.

reverse方法

public StringBuffer reverse()

Causes this character sequence to be replaced by the reverse of the sequence. If there are any surrogate pairs included in the sequence, these are treated as single characters for the reverse operation. Thus, the order of the high-low surrogates is never reversed. Let n be the character length of this character sequence (not the length in char values) just prior to execution of the reverse method. Then the character at index k in the new character sequence is equal to the character at index n-k-1 in the old character sequence.

  • 返回

    • a reference to this object.

StringBuffer的截取功能

substring方法

public String substring(int start)

Returns a new String that contains a subsequence of characters currently contained in this character sequence. The substring begins at the specified index and extends to the end of this sequence.

  • 参数

    • start - The beginning index, inclusive.
  • 返回

    • The new string.
public String substring(int start,int end)

Returns a new String that contains a subsequence of characters currently contained in this sequence. The substring begins at the specified start and extends to the character at index end - 1

  • 参数

    • start - The beginning index, inclusive.
    • end - The ending index, exclusive.
  • 返回

    • The new string.

举几个例子

package com.guiyang.restudy3;public class D4StringBuffer {public static void main(String[] args) {Demo1();Demo2();       }private static void Demo2() {System.out.println("-------");StringBuffer sb = new StringBuffer("HuangBlack");      String str = sb.substring(5);    //返回值类型不再是StringBuffer,而是StringSystem.out.println(str);System.out.println(sb);System.out.println("-------");String str3 = sb.substring(5, 7);System.out.println(str3);}private static void Demo1() {StringBuffer sb = new StringBuffer("HuangBlack");       sb.replace(0, 5, "Bai");                  //替换System.out.println(sb);sb.reverse();System.out.println(sb);}}

输出:

BaiBlack
kcalBiaB
-------
Black
HuangBlack
-------
Bl

第三次学JAVA再学不好就吃翔(part56)--StringBuffer的替换反转和截取功能相关推荐

  1. 第三次学JAVA再学不好就吃翔(part107)--字符流的拷贝

    学习笔记,仅供参考,有错必纠 文章目录 IO流 字符流的拷贝 拷贝纯文本文件 注意事项 自定义字符数组的拷贝 IO流 字符流的拷贝 拷贝纯文本文件 zzz.txt: 安徽财经大学 JAVA代码: pa ...

  2. 第三次学JAVA再学不好就吃翔(part106)--字符流

    学习笔记,仅供参考,有错必纠 文章目录 IO流 字符流输入流FileReader 字符流是什么 FileReader类构造方法 举个例子 字符流输出流FileWriter FileWriter构造方法 ...

  3. 第三次学JAVA再学不好就吃翔(part46)--Scanner类

    学习笔记,仅供参考 文章目录 Scanner类 Scanner获取数据出现的问题 Scanner类 概述 Scanner是在java.util包下的一个类,使用该类时我们需要导包.这个类被final修 ...

  4. 第三次学JAVA再学不好就吃翔(part117)--单例设计模式

    学习笔记,仅供参考,有错必纠 文章目录 多线程 单例设计模式 饿汉式 懒汉式 第三种格式 Timer类 线程的五种状态 多线程 单例设计模式 单例设计模式,即保证类在内存中只有一个对象,如何保证类在内 ...

  5. 第三次学JAVA再学不好就吃翔(part109)--LineNumberReader类

    学习笔记,仅供参考,有错必纠 IO流 LineNumberReader类 该类继承自java.io.BufferedReader,该类可以跟踪行号的缓冲字符输入流.此类定义了方法 setLineNum ...

  6. 第三次学JAVA再学不好就吃翔(part108)--带缓冲的字符流

    学习笔记,仅供参考,有错必纠 文章目录 IO流 带缓冲的字符流 BufferedReader和BufferedWriter readLine和newLine方法 IO流 带缓冲的字符流 Buffere ...

  7. 第三次学JAVA再学不好就吃翔(part7)--基础语法之运算符

    学习笔记,仅供参考 文章目录 JAVA基础语法 运算符 算数运算符 算术运算符 ++和-- 赋值运算符 关系运算符 JAVA基础语法 运算符 什么是运算符 就是对常量和变量进行操作的符号. 运算符的分 ...

  8. 第三次学JAVA再学不好就吃翔(part115)--多线程

    学习笔记,仅供参考,有错必纠 文章目录 多线程 多线程引入 什么是线程 多线程并行和并发的区别 Java程序运行原理 多线程的实现方式 多线程实现的方式1 多线程程序实现的方式2 以上两种实现方式的区 ...

  9. 第三次学JAVA再学不好就吃翔(part113)--对象操作流

    学习笔记,仅供参考,有错必纠 对象操作流 对象操作流可以将一个对象写出, 或者读取一个对象到程序中,也就是执行了序列化和反序列化的操作. 举个例子 先构造一个Role类: package com.gu ...

最新文章

  1. 如何使用JPA注解标注多对多的关系
  2. python怎么处理数据_python中scrapy处理项目数据的实例分析
  3. 解决IE6下不支持 png24的透明图片问题
  4. ExtendTableView
  5. YOLOv4重磅发布,五大改进,二十多项技巧实验,堪称最强目标检测万花筒
  6. html中input、label、form、textarea、select
  7. Redis进阶实践之十八 使用管道模式提高Redis查询的速度
  8. 中文文字校对和文档对比合并开源工具调研
  9. 服务器安装固态硬盘的步骤,服务器系统安装中不识别固态硬盘问题
  10. 良心安利三大游戏音效素材网站
  11. 怎么给边框添加图片 css3,用CSS3轻松实现图片边框
  12. 矩形波的傅里叶级数及代码
  13. 芯片设计五部曲之一 | 声光魔法师——模拟IC
  14. 在职计算机培训班,计算机科学与技术在职研究生招生院校有哪些?
  15. [激光原理与应用-60]:激光器 - 光学 - 光的四大理论框架与其层次:几何光学、波动光学、电磁光学、电子光学
  16. 蓝牙耳机哪款好用?2020高性能低延迟游戏蓝牙耳机推荐
  17. Blender齿轮制作
  18. opencv python 灰度图转三通道彩色图
  19. 计算机行业岗位排名,全国“最缺工”100个职业排行,这些专业就业率高,发展前景好...
  20. Transformer解读

热门文章

  1. python版本切换_Ubuntu18.04下python版本完美切换的解决方法
  2. 爬取json Swaggerui界面
  3. HTML5 CSS3初学者指南(3) – HTML5新特性
  4. 利用Redis实现消息队列原理
  5. Python-基础-时间日期处理小结
  6. 腾讯云大数据套件Hermes-MR索引插件使用总结
  7. ajax传参到实体类对应字段
  8. 电子商务系统的设计与实现(九):后端管理系统功能细化
  9. set 赋值(转载)
  10. jQuery中操作元素节点appendTo()与prependTo()的区别