(JAVA)StringBuffer类
可变数组:StringBuffer类(静态缓冲区) 提高字符串的操作效率 StringBuffer 底层实现原理是字符数组,没有final StringBuffer,含有数组char[] value 默认长度是16 在JAVA中,数组是固定长度,一旦创建不能改变 StringBuffer 通过数组的赋值,实现数组的可变性,但数组本身不能改变长度 1.将任意数据类型,添加到缓冲区中append() 2.返回一个StringBuffer,因此可以使用方法调用链 3.将任意数据,插入到缓冲区的某一个索引上index(索引,插入内容) 4.缓冲区的删除方法:delete(int start, int end);deleteCharAt( int index); 5.修改缓冲区的单个字符setCharAt( int index ,char ch); 6.缓冲区字符串翻转,reverse() 7,缓冲区字符串截取,substring()返回的是String结构 8.将缓冲区变成不可变对象toString() 9.字符串对象与缓冲区对象互转 字符串变成缓冲区 String s = new String("asdf"); StringBuffer b = new StringBuffer(); b.append(s);缓冲区变成字符串 StringBuffer buffer = new StringBuffer("vbnm"); String string = buffer.toString(); 注意返回值是String 要接收变量
/*
public final class StringBufferextends AbstractStringBuilderimplements java.io.Serializable, CharSequenceA cache of the last value returned by toString. Clearedwhenever the StringBuffer is modified.private transient char[] toStringCache;use serialVersionUID from JDK 1.0.2 for interoperability
static final long serialVersionUID = 3388685877147921107L;/*** Constructs a string buffer with no characters in it and an* initial capacity of 16 characters.public StringBuffer() {super(16);}/*** Constructs a string buffer with no characters in it and* the specified initial capacity.** @param capacity the initial capacity.* @exception NegativeArraySizeException if the {@code capacity}* argument is less than {@code 0}.public StringBuffer(int capacity) {super(capacity);}/*** Constructs a string buffer initialized to the contents of the* specified string. The initial capacity of the string buffer is* {@code 16} plus the length of the string argument.** @param str the initial contents of the buffer.public StringBuffer(String str) {super(str.length() + 16);append(str);}/*** Constructs a string buffer that contains the same characters* as the specified {@code CharSequence}. The initial capacity of* the string buffer is {@code 16} plus the length of the* {@code CharSequence} argument.* <p>* If the length of the specified {@code CharSequence} is* less than or equal to zero, then an empty buffer of capacity* {@code 16} is returned.** @param seq the sequence to copy.* @since 1.5public StringBuffer(CharSequence seq) {this(seq.length() + 16);append(seq);}*/import java.util.Scanner;public class test {public static void main(String[] args) {//System.out.println(getStringCont());//System.out.println(getString_1());//System.out.println(getString_2());//getString_3();//System.out.println(getString_4());System.out.println(getString_5());}//定义一个共能,从一个字符串中查找另一个字符串出现的次数public static int getStringCont() {Scanner sc1 = new Scanner(System.in);System.out.println("请输入一个字符串:");String s1 = sc1.nextLine();Scanner sc2 = new Scanner(System.in);System.out.println("请输入要查找的内容:");String s2 = sc2.nextLine();//定义计数器int count = 0;//定义下角标int index = 0;if (s1.length() == 0 || s2.length() == 0)return count;else {//判断s1第一次出现的位置,只要第一次出现的位置大于零,就进入循环while((index = s1.indexOf(s2))>0){count++;//新的字符串长度为查找内容后的下角标到结尾s1 = s1.substring(index+s2.length());}}return count;}public static StringBuffer getString_1(){StringBuffer buffer = new StringBuffer("abc");buffer.append("sdf");buffer.insert(2,"bnm");return buffer;}public static StringBuffer getString_2(){StringBuffer buffer_1 = new StringBuffer();buffer_1.append("poiu");System.out.println(buffer_1+"1");//删除指定下角标buffer_1.deleteCharAt(1);System.out.println(buffer_1+"2");//删除一堆buffer_1.delete(0,2);return buffer_1;}public static void getString_3(){StringBuffer buffer_2 = new StringBuffer();buffer_2.append("abcdefg");buffer_2.setCharAt(3,'p');System.out.println(buffer_2);}public static StringBuffer getString_4(){String s = "asdf";StringBuffer b = new StringBuffer();b.append(s);return b;}public static String getString_5(){StringBuffer buffer = new StringBuffer("vbnm");String string = buffer.toString();return string;}
(JAVA)StringBuffer类相关推荐
- Java StringBuffer类
如果经常需要对一个字符串进行修改,例如插入.删除.拼接等操作,使用SringBuffer更加合适,因为StringBuffer在进行字符串处理是不生成新的对象,在内存上由于String类. Strin ...
- java StringBuffer类 常用方法
为什么80%的码农都做不了架构师?>>> SringBuffer是字符串缓冲区 是一个容器 "面盆理论" 相比数组的优势: 长度是可以变化的 可以操作多个数 ...
- Java StringBuffer类的一些常用方法
目录 1.append(String s) 2.delete(int start, int end) 3.insert() 4.replace(int start, int end, String s ...
- Java学习总结:31(StringBuffer类)
StringBuffer类 在Java中String类不适合使用于频繁修改字符串的操作上(因为其字符串常量一旦声明则不可改变,只能改变字符串对象,改变的是其内存地址的指向),所以我们可以使用Strin ...
- JAVA的StringBuffer类
StringBuffer类和String一样,也用来代表字符串,只是由于StringBuffer的内部实现方式和String不同,所以StringBuffer在进行字符串处理时,不生成新的对象,在内存 ...
- java API(String类 和 StringBuffer类)
java api 指的是jdk中提供的各种功能的java类. 在java中定义了String和StringBuffer两个类来包装字符串,并提供了一系列操作字符串的方法,他们都位于java.long包 ...
- JAVA API-----String类和StringBuffer类
String类和StringBuffer类主要用来处理字符串,这两个类提供了很多字符串的使用处理方法.String类是不可变类,表示对象所包含的字符串类不能改变.StringBuffer类是可变类,其 ...
- [JAVA基础类库] String类 ○ StringBuffer类 ○ StringBuilder类
引言 字符串就是一连串的字符序列,Java提供了String.StringBuffer和StringBuilder三个类来封装对字符串,并提供了系列方法来操作字符串对象. String类是不可变类的: ...
- Java之StringBuffer类
Java之StringBuffer类 SringBuffer类 当对字符串进行修改的时候,需要使用StringBuffer和StringBuilder类. Sting类的区别 它们和String类不同 ...
- Java常用类之String类、Stringbuffer和Random类练习
定义一个StringBuffer类对象, 1)使用append方法向对象中添加26个字母,并倒序遍历输入 2)删除前五个字符 package 第十一章常用类; /*** 定义一个StringBuffe ...
最新文章
- 机器学习_生成式模型与判别式模型
- MySQL与优化有关的命令_MySQL优化全攻略-相关数据库命令
- win7 storm搭建
- Linux下使用expect实现跳板机自动跳转/免密登录/自动登录(转)
- python print用法制表空格_python中print函数的输出问题(空格,制表符)
- android OKHttp的基本使用详解
- 计算机osta试题,OSTA试题库.doc
- android新建项目错误,新建Android项目出错
- 【VS2010学习笔记】【异常处理】general error c1010070: Failed to load and parse the manifest.
- 安卓学习pdf_【手机电脑全平台通用】手把手教你制作可点读日语PDF!
- 学生成绩管理系统c语言直方图,Excel表格的25招必学秘技 电子表格常用技巧大全...
- Echart3绘制世界地图连线中国城市
- 关于百度地图和高德地图,关于地图坐标系
- Android极速从视频里提取音频
- 【技术方案】如何基于高清摄像头设备,搭建慢直播平台?
- FLOTHERM 10.1软件热分析仿真基础培训 风冷 水冷 自冷视频教程
- 使用Jsch执行Shell脚本
- 用友通10.1禁止安装在SQL2005
- 光纤的损耗机理 散射损耗 吸收损耗 弯曲损耗
- 如何写好一篇产品测评报告
热门文章
- html:(32):字体,字号,颜色
- linux 使用paho C库实现mqtt客户端
- Qt ModbusTCP ModbusRTU 使用同步读和异步写
- 优地机器人厂家_2019中国人工智能机器人企业TOP30榜单发布 优地科技跻身前十...
- python转c报错no module named_python异常No module named 'win32com'
- 【刷题】洛谷 P2709 小B的询问
- Spark实战之读写HBase
- Python学习笔记——基础篇【第七周】———FTP作业(面向对象编程进阶 Socket编程基础)...
- PHP的time函数返回时间不正确
- 通过CMD命令行创建和使用Android 模拟器 AVD