博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java基础入门 StringBuffer类
阅读量:3890 次
发布时间:2019-05-23

本文共 1127 字,大约阅读时间需要 3 分钟。

由于字符串是常量,一旦创建,其内容及长度就不可改变。如果需要改变则只能创建新的字符串。为了便于对字符串的修改,在JDK中提供了StringBuffer类。与String最大的区别在于可以修改内容及长度。具体用法见下边代码示例:

public class Main{	public static void main(String[] args)throws Exception{		System.out.println("1.添加操作...............");		add();		System.out.println("2.删除操作...............");		remove();		System.out.println("3.修改操作...............");		alter();	}	public static void add() {		StringBuffer sb=new StringBuffer();//创建对象		sb.append("abcdefg");//在末尾添加字符		System.out.println("append添加结果:"+sb);		sb.insert(2, "123");//在指定位置添加字符		System.out.println("insert插入结果:"+sb);	}	public static void remove() {		StringBuffer sb=new StringBuffer("abcdefg");		sb.delete(1, 5);     //删除指定范围		System.out.println("删除指定位置后的结果:"+sb);		sb.deleteCharAt(2);  //删除指定位置		System.out.println("删除指定位置后的结果:"+sb);		sb.delete(0, sb.length());		System.out.println("清空后的结果:"+sb);	}	public static void alter() {		StringBuffer sb=new StringBuffer("abcdefg");		sb.setCharAt(1,'p');     //修改指定位置字符		System.out.println("修改后的结果:"+sb);		sb.replace(1, 3, "qq");  //替换指定位置字符串或字符		System.out.println("修改后的结果:"+sb);		System.out.println("字符串反转后的结果:"+sb.reverse());	}}

 

转载地址:http://fuohn.baihongyu.com/

你可能感兴趣的文章
theano 后端爆内存
查看>>
os.environ 和 keras.json
查看>>
后台面试经典问题-手写LRU算法
查看>>
Part-Guided Attention Learning for Vehicle Instance Retrieval
查看>>
Deep Residual Learning for Image Recognition
查看>>
Bag of Tricks and A Strong Baseline for Deep Person Re-identification
查看>>
vue+flask实现视频目标检测yolov5
查看>>
关于BigInteger
查看>>
UIScrollView不能响应UITouch事件
查看>>
iOS TextFiled 文本密码切换 光标偏移解决
查看>>
iOS 当前应用所占内存和设备可用内存
查看>>
iOS 文件属性
查看>>
UIView的layoutSubviews和drawRect方法何时调用
查看>>
iOS GCD多线程下载原理
查看>>
NSData全部API解释
查看>>
iOS 侧滑菜单封装Demo(类似QQ侧滑效果)
查看>>
Spring学习(二)
查看>>
Spring学习(三)
查看>>
Spring学习(四)
查看>>
java解惑——易错知识点归纳总结
查看>>