JAVA中StringBuffer类常用方法
String是不变类,用String修改字符串会新建一个String对象,如果频繁的修改,将会产生很多的String对象,开销很大.因此java提供了一个StringBuffer类,这个类在修改字符串方面的效率比String高了很多。
在java中有3个类来负责字符的操作。
1.Character 是进行单个字符操作的,
2.String 对一串字符进行操作。不可变类。
3.StringBuffer 也是对一串字符进行操作,但是可变类。
- public class UsingStringBuffer {
- /**
- * 查找匹配字符串
- */
- public static void testFindStr() {
- StringBuffer sb = new StringBuffer();
- sb.append("This is a StringBuffer");
- // 返回子字符串在字符串中最先出现的位置,如果不存在,返回负数
- System.out.println("sb.indexOf(\"is\")=" + sb.indexOf("is"));
- // 给indexOf方法设置参数,指定匹配的起始位置
- System.out.println("sb.indexOf(\"is\")=" + sb.indexOf("is", 3));
- // 返回子字符串在字符串中最后出现的位置,如果不存在,返回负数
- System.out.println("sb.lastIndexOf(\"is\") = " + sb.lastIndexOf("is"));
- // 给lastIndexOf方法设置参数,指定匹配的结束位置
- System.out.println("sb.lastIndexOf(\"is\", 1) = "
- + sb.lastIndexOf("is", 1));
- }
- /**
- * 截取字符串
- */
- public static void testSubStr() {
- StringBuffer sb = new StringBuffer();
- sb.append("This is a StringBuffer");
- // 默认的终止位置为字符串的末尾
- System.out.print("sb.substring(4)=" + sb.substring(4));
- // substring方法截取字符串,可以指定截取的起始位置和终止位置
- System.out.print("sb.substring(4,9)=" + sb.substring(4, 9));
- }
- /**
- * 获取字符串中某个位置的字符
- */
- public static void testCharAtStr() {
- StringBuffer sb = new StringBuffer("This is a StringBuffer");
- System.out.println(sb.charAt(sb.length() - 1));
- }
- /**
- * 添加各种类型的数据到字符串的尾部
- */
- public static void testAppend() {
- StringBuffer sb = new StringBuffer("This is a StringBuffer!");
- sb.append(1.23f);
- System.out.println(sb.toString());
- }
- /**
- * 删除字符串中的数据
- */
- public static void testDelete() {
- StringBuffer sb = new StringBuffer("This is a StringBuffer!");
- sb.delete(0, 5);
- sb.deleteCharAt(sb.length() - 1);
- System.out.println(sb.toString());
- }
- /**
- * 向字符串中插入各种类型的数据
- */
- public static void testInsert() {
- StringBuffer sb = new StringBuffer("This is a StringBuffer!");
- // 能够在指定位置插入字符、字符数组、字符串以及各种数字和布尔值
- sb.insert(2, 'W');
- sb.insert(3, new char[] { 'A', 'B', 'C' });
- sb.insert(8, "abc");
- sb.insert(2, 3);
- sb.insert(3, 2.3f);
- sb.insert(6, 3.75d);
- sb.insert(5, 9843L);
- sb.insert(2, true);
- System.out.println("testInsert: " + sb.toString());
- }
- /**
- * 替换字符串中的某些字符
- */
- public static void testReplace() {
- StringBuffer sb = new StringBuffer("This is a StringBuffer!");
- // 将字符串中某段字符替换成另一个字符串
- sb.replace(10, sb.length(), "Integer");
- System.out.println("testReplace: " + sb.toString());
- }
- /**
- * 将字符串倒序
- */
- public static void reverseStr() {
- StringBuffer sb = new StringBuffer("This is a StringBuffer!");
- System.out.println(sb.reverse()); // reverse方法将字符串倒序
- }
- }
小结:
StringBuffer不是不变类,在修改字符串内容时,不会创建新的对象,因此,它比String类更适合修改字符串。
StringBuffer类没有提供同String一样的toCharArray方法
StringBuffer类的replace方法同String类的replace方法不同,它的replace方法有三个参数,第一个参数指定被替换子串的起始位置,第二个参数指定被替换子串的终止位置,第三个参数指定新子串
JAVA中StringBuffer类常用方法的更多相关文章
- JAVA中StringBuffer类常用方法详解
String是不变类,用String修改字符串会新建一个String对象,如果频繁的修改,将会产生很多的String对象,开销很大.因此java提供了一个StringBuffer类,这个类在修改字符串 ...
- Java中StringBuffer类的常用方法
StringBuffer:StringBuffer类型 描述:在实际应用中,经常回遇到对字符串进行动态修改.这时候,String类的功能受到限制,而StringBuffer类可以完成字符串的动态添加. ...
- JAVA中String类常用方法 I
String类常用方法有: int length() -– 返回当前字符串的长度 int indexOf(int ch) -– 查找ch字符在该字符串中第一次出现的位置 int indexOf(Str ...
- Java中StringBuffer类
StringBuffer: 线程安全的可变字符串. StringBuffer和String的区别?前者长度和内容可变,后者不可变.如果使用前者做字符串的拼接,不会浪费太多的资源. StringBuff ...
- Java中StringBuffer类append方法的使用
public static void testAppend() { StringBuffer sb = new StringBuffer("This is a StringBuffer!&q ...
- Java中 ArrayList类常用方法和遍历
ArrayList类对于元素的操作,基本体现在——增.删.查.常用的方法有: public boolean add(E e) :将指定的元素添加到此集合的尾部. public E remove(in ...
- Java中String类常用方法(字符串中的子字符串的个数)
重点内容 4种方法: 1.int indexOf(String str)返回第一次出现的指定子字符串在此字符串中的索引. 2.int indexOf(String str, int startInde ...
- java中File类的常用方法总结
java中File类的常用方法 创建: createNewFile()在指定的路径创建一个空文件,成功返回true,如果已经存在就不创建,然后返回false. mkdir() 在指定的位置创建一个此抽 ...
- 【转】JAVA的StringBuffer类
[转]JAVA的StringBuffer类 StringBuffer类和String一样,也用来代表字符串,只是由于StringBuffer的内部实现方式和String不同,所以StringBu ...
随机推荐
- Button MouseEvent颜色变化
public partial class Form1 : Form { public Form1() { InitializeComponent(); this.button1.Enter += bu ...
- java多线程有几种实现方法,都是什么?同步有几种实现方法,都是什么?
多线程有两种实现方法,分别是继承Thread类与实现Runnable接口 同步的实现方面有两种,分别是synchronized,wait与notify 先看一下java线程运行时各个阶段的运行状态 j ...
- prologue epilogue
https://www.hackerschool.com/blog/7-understanding-c-by-learning-assemblyhttps://www.hackerschool.com ...
- 控制textbook输入字符
在KeyPress事件中假如如下代码此实例表示可输入数字退格和“.”. 具体字符KeyChar见连接 http://www.cnblogs.com/linji/archive/2012/10/24/2 ...
- 类似java.lang.NoClassDefFoundError: org/jaxen/JaxenException解决方法
在使用dom4j的xpath时出现java.lang.NoClassDefFoundError: org/jaxen/JaxenException的异常,原因是dom4j引用了jaxen jar包,而 ...
- Extjs4 up 和down的用法
Extjs4.x中,每个组件都新增加了两个方法up()和down()方法.这两个方法都是用来获取组件的,下面我们来看下up()方法和down()方法的官方解释. Extjs4.x中,新增加了两个方法u ...
- python------unicode字符串转换为其他类型
问题描述: 一下字符串转换为json类型 {u'src': u'crawl', u'cid': u'Ctengbangguoji', u'datatype': u'ItemBase', u'times ...
- 十大滤波算法程序大全(Arduino精编无错版)(转)
源:十大滤波算法程序大全(Arduino精编无错版) 转载请注明出处:极客工坊 http://www.geek-workshop.com/thread-7694-1-1.html
- bzoj3110: [Zjoi2013]K大数查询 【cdq分治&树套树】
模板题,折腾了许久. cqd分治整体二分,感觉像是把询问分到答案上. #include <bits/stdc++.h> #define rep(i, a, b) for (int i = ...
- SD卡初始化以及命令详解
SD卡是嵌入式设备中很常用的一种存储设备,体积小,容量大,通讯简单,电路简单所以受到很多设备厂商的欢迎,主要用来记录设备运行过程中的各种信息,以及程序的各种配置信息,很是方便,有这样几点是需要知道的 ...