继承了AbstractStringBuilder类
实现了java.io.Serializable, CharSequence接口

一、常用用法

1.1 在指定位置插入字符串

使用insert函数

public class test {public static void main(String[] args) {StringBuilder sb=new StringBuilder("012345");sb.insert(2,"insert");System.out.println(sb.toString());}
}

输出:

01insert2345

1.2 删除某些字符

使用delete或者deleteCharAt函数

public class test {public static void main(String[] args) {String temp="0123456";StringBuilder sb=new StringBuilder(temp);sb.deleteCharAt(6);System.out.println(sb.toString());sb.delete(0,2);System.out.println(sb.toString());}}

输出:

012345
2345

1.3 修改某个位置的字符

使用replace函数

public class test {public static void main(String[] args) {String temp="0123456";StringBuilder sb=new StringBuilder(temp);sb.replace(1,2,"test");System.out.println(sb.toString());}}

输出:

0test23456

一、属性

二、方法

2.1 toString

@Overridepublic String toString() {// Create a copy, don't share the arrayreturn new String(value, 0, count);}

2.2 delete

/*** @throws StringIndexOutOfBoundsException {@inheritDoc}*/@Overridepublic StringBuilder delete(int start, int end) {super.delete(start, end);return this;}

2.3 deleteCharAt

/*** @throws StringIndexOutOfBoundsException {@inheritDoc}*/@Overridepublic StringBuilder deleteCharAt(int index) {super.deleteCharAt(index);return this;}

2.4 append

@Overridepublic StringBuilder append(String str) {super.append(str);return this;}

2.5 getChars

/*** Copies characters from this string into the destination character* array.* <p>* The first character to be copied is at index {@code srcBegin};* the last character to be copied is at index {@code srcEnd-1}* (thus the total number of characters to be copied is* {@code srcEnd-srcBegin}). The characters are copied into the* subarray of {@code dst} starting at index {@code dstBegin}* and ending at index:* <blockquote><pre>*     dstBegin + (srcEnd-srcBegin) - 1* </pre></blockquote>** @param      srcBegin   index of the first character in the string*                        to copy.* @param      srcEnd     index after the last character in the string*                        to copy.* @param      dst        the destination array.* @param      dstBegin   the start offset in the destination array.* @exception IndexOutOfBoundsException If any of the following*            is true:*            <ul><li>{@code srcBegin} is negative.*            <li>{@code srcBegin} is greater than {@code srcEnd}*            <li>{@code srcEnd} is greater than the length of this*                string*            <li>{@code dstBegin} is negative*            <li>{@code dstBegin+(srcEnd-srcBegin)} is larger than*                {@code dst.length}</ul>*/public void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin) {if (srcBegin < 0) {throw new StringIndexOutOfBoundsException(srcBegin);}if (srcEnd > value.length) {throw new StringIndexOutOfBoundsException(srcEnd);}if (srcBegin > srcEnd) {throw new StringIndexOutOfBoundsException(srcEnd - srcBegin);}System.arraycopy(value, srcBegin, dst, dstBegin, srcEnd - srcBegin);}

2.6 insert

在指定偏移量后插入一个String字符串

/*** @throws StringIndexOutOfBoundsException {@inheritDoc}*/@Overridepublic StringBuilder insert(int offset, String str) {super.insert(offset, str);return this;}

在指定偏移量后面插入一个char字符

/*** @throws StringIndexOutOfBoundsException {@inheritDoc}*/@Overridepublic StringBuilder insert(int offset, char[] str) {super.insert(offset, str);return this;}

2.7 replace

/*** @throws StringIndexOutOfBoundsException {@inheritDoc}*/@Overridepublic StringBuilder replace(int start, int end, String str) {super.replace(start, end, str);return this;}

jdk StringBuilder实现相关推荐

  1. 第 8 章 建造者模式

    第 8 章 建造者模式 1.盖房项目实际需求 需要建房子:这一过程为打桩. 砌墙.封顶 房子有各种各样的,比如普通房,高楼,别墅,各种房子的过程虽然一样,但是各自实现的细节不同 请编写程序,完成需求 ...

  2. JDK源码解析之 Java.lang.StringBuilder

    StringBuilder类表示一个可变的字符序列.StringBuilder的API与StringBuffer互相兼容,但是StringBuilder是非线程安全的,在大多数实现中它比StringB ...

  3. JDK源码系列(6)-StringBuilder

    一.概述 StringBuilder是一个可变的字符串序列,这个类被设计去兼容StringBuffer类的API,但不保证线程安全性,是StringBuffer单线程情况下的一个替代实现.在可能的情况 ...

  4. 【JDK源码分析】StringBuilder、StringBuilder、String、AbstractStringBuilder源码解析

    前言 String为不可变,StringBuilder.StringBuffer都为可变. 下面是它们之前的关系 为什么String是不可变的? // final修饰,禁止继承String publi ...

  5. 深入JDK源码,这里总有你不知道的知识点!

    Java的基础知识有很多,但是我认为最基础的知识应该要属jdk的基础代码,jdk的基础代码里面,有分了很多基础模块,其中又属jdk包下面的lang包最为基础. 我们下面将总结和分析一下lang包下面最 ...

  6. String、StringBuffer、StringBuilder的理解

    问题: 理解 Java的字符串,String.StringBuffer.StringBuilder 有什么区别? 知识点 字符串设计和实现考量 String是Immutable(线程安全.字符串常量池 ...

  7. StringBuilder、StringBuffer、String区别

    相信大家对 String 和 StringBuffer 的区别也已经很了解了,但是估计还是会有很多同志对这两个类的工作原理有些不清楚的地方,今天重新把这个概念给大家复习一下,顺便牵出 J2SE5.0 ...

  8. 一篇与众不同的 String、StringBuilder 和 StringBuffer 详解

    1 碎碎念  这是一道老生常谈的问题了,字符串是不仅是 Java 中非常重要的一个对象,它在其他语言中也存在.比如 C++.Visual Basic.C# 等.字符串使用 String 来表示,字符串 ...

  9. 2.Java中String,StringBuilder以及StringBuffer的关系与区别

    String     StringBuffer     StringBuilder String的值是不可变的,这就导致每次对String的操作都会生成新的String对象,不仅效率低下,而且浪费大量 ...

  10. 一个 P4 的 Bug,就难倒了 JDK 吗 ?

    作者 | 码农唐磊 来源 | 程序猿石头(ID:tangleithu) 背景 分享一下之前踩的一个坑,背景是这样的: 我们的项目依赖于一个外部服务,该外部服务提供 REST 接口供我方调用,这是很常见 ...

最新文章

  1. linux 6.4 安装dns,Linux 轻松上手 架设 CentOS 6.4 DNS+FTP ndash;(六)、安装设定vsftp
  2. WM_CHAR、WM_KEYDOWN和WM_SYSKEYDOWN消息
  3. UITabBarController 笔记(三) UITabBarController 配合 UINavigationController 的使用
  4. 禅道xampp文件夹无法删除_无法删除文件解决方案
  5. 十九、python沉淀之路--装饰器
  6. 使用计算机打印汉子文档,电子科技大学《计算机应用基础(本科)》20春期末考试【标准答案】...
  7. Debian8安装TeamViewer远程协助软件
  8. APP原型设计利器-墨刀MockingBot
  9. imagej得到灰度图数据_imageJ 使用教程之样本长度测量
  10. 【如何7天写完一篇发明专利】
  11. 记录一直以来看过的电视剧、电影及书籍
  12. 苹果微信分身版ios_香草直播苹果版下载-香草直播ios苹果版「精彩直播」
  13. Vue UI组件 开发框架 服务端 辅助工具 应用实例 Demo示例
  14. iFunk翼超极本亲测心得
  15. Chevereto图床搭建 | 利用云服务器搭建免费图床完整教程
  16. uci数据集中的缺失数据_从uci早期糖尿病风险预测数据集中创建分类器
  17. 问题分析报告--简单SQL启动MR
  18. 对于微软学术搜索的评价——陈稳霖
  19. 公众号话题标签怎样添加
  20. #内存泄露# #valgrind# valgrind简介

热门文章

  1. mysql命令行进入报错ERROR 2002 (HY000)
  2. JavaEE学习--javascript中的正则表达式
  3. Android ViewDragHelper的简单分析(一)
  4. 30万奖金等你拿!Apache Flink 极客挑战赛入门指南(附Demo)
  5. 面试鹅厂,我被虐的体无完肤。。。
  6. Java基础---Java---IO流-----File 类、递归、删除一个带内容的目录、列出指定目录下文件夹、FilenameFilte
  7. python可迭代对象相关的内建函数_python之函数闭包、可迭代对象和迭代器
  8. python流星雨代码_用python一起来看流星雨
  9. python速学_【Python杂货铺】速学python基础
  10. python中tkinter模块_Python模块:tkinter