顺德顺的网站建设,网络服务器类型,想做电商带货怎么入手,2022最新泛站群程序⭐ 作者#xff1a;小胡_不糊涂 #x1f331; 作者主页#xff1a;小胡_不糊涂的个人主页 #x1f4c0; 收录专栏#xff1a;浅谈Java #x1f496; 持续更文#xff0c;关注博主少走弯路#xff0c;谢谢大家支持 #x1f496; String 1. 修改字符串2. StringBuilder和… ⭐ 作者小胡_不糊涂 作者主页小胡_不糊涂的个人主页 收录专栏浅谈Java 持续更文关注博主少走弯路谢谢大家支持 String 1. 修改字符串2. StringBuilder和StringBuffer 1. 修改字符串
上一篇文章提到String是一种不可变对象。
那么想要修改字符串中内容该如何操作呢
先看一段代码
public static void main(String[] args) {String s hello;s world;System.out.println(s); // 输出hello world
}上述代码使用追加方式来修改字符串s的内容虽然可以成功修改但这种方式不推荐使用因为它的效率非常低中间创建了好多临时对象 由此可知在对String类进行修改时效率是非常慢的因此要尽量避免对String的直接需要如果要修改建议使用StringBuffer或者StringBuilder。
2. StringBuilder和StringBuffer
由于String的不可更改特性为了方便字符串的修改Java中又提供StringBuilder和StringBuffer类。这两个类大部分功能是相同的。
StringBuilder常用的一些方法
方法功能StringBuff append(Stringstr)在尾部追加相当于String的可以追加boolean、char、char[]、double、float、int、long、Object、String、StringBuff的变量char charAt(int index)获取index位置的字符int length()获取字符串的长度int capacity()获取底层保存字符串空间总的大小void ensureCapacity(int mininmumCapacity)扩容void setCharAt(int index,char ch)将index位置的字符设置为chint indexOf(String str)返回str第一次出现的位置int indexOf(String str, int fromIndex)从fromIndex位置开始查找str第一次出现的位置int lastIndexOf(String str)返回最后一次出现str的位置int lastIndexOf(String str,int fromIndex)从fromIndex位置开始找str最后一次出现的位置StringBuff insert(int offset, String str)在offset位置插入八种基类类型 String类型 Object类型数据StringBuffer deleteCharAt(int index)删除index位置字符StringBuffer delete(int start, int end)删除[start, end)区间内的字符StringBuffer replace(int start, int end, String str)将[start, end)位置的字符替换为strString substring(int start)从start开始一直到末尾的字符以String的方式返回String substring(int start,int end)将[start,end)范围内的字符以String的方式返回StringBuffer reverse()反转字符串String toString()将所有字符按照String的方式返回
使用实例
public static void main(String[] args) {StringBuilder sb1 new StringBuilder(hello);StringBuilder sb2 sb1;// 追加即尾插--字符、字符串、整形数字sb1.append( ); // hellosb1.append(world); // hello worldsb1.append(123); // hello world123System.out.println(sb1); // hello world123System.out.println(sb1 sb2); // trueSystem.out.println(sb1.charAt(0)); // 获取0号位上的字符 hSystem.out.println(sb1.length()); // 获取字符串的有效长度 14System.out.println(sb1.capacity()); // 获取底层数组的总大小 21sb1.setCharAt(0, H); // 设置任意位置的字符 Hello world123sb1.insert(0, Hello world!!!); // Hello world!!!Hello world123System.out.println(sb1);//Hello world!!!Hello world123System.out.println(sb1.indexOf(Hello)); // 获取Hello第一次出现的位置 0System.out.println(sb1.lastIndexOf(hello)); // 获取hello最后一次出现的位置 -1sb1.deleteCharAt(0); // 删除首字符sb1.delete(0,5); // 删除[0, 5)范围内的字符String str sb1.substring(0, 5); // 截取[0, 5)区间中的字符以String的方式返回System.out.println(str);//worldsb1.reverse(); // 字符串逆转str sb1.toString(); // 将StringBuffer以String的方式返回System.out.println(str);//321dlrow olleH!!!dlrow}从上述例子可以看出String和StringBuilder最大的区别在于String的内容无法修改而StringBuilder的内容可以修改。频繁修改字符串的情况考虑使用StringBuilder。 注String和StringBuilder类不能直接转换。如果要想互相转换可以采用如下原则:
String变为StringBuilder利用StringBuilder的构造方法或append()方法StringBuilder变为String调用toString()方法
String、StringBuffer、StringBuilder的区别
String的内容不可修改StringBuffer与StringBuilder的内容可以修改。StringBuffer与StringBuilder大部分功能是相似的。StringBuffer采用同步处理属于线程安全操作而StringBuilder未采用同步处理属于线程不安全操作。 文章转载自: http://www.morning.myfwb.cn.gov.cn.myfwb.cn http://www.morning.fhlfp.cn.gov.cn.fhlfp.cn http://www.morning.gbhsz.cn.gov.cn.gbhsz.cn http://www.morning.fpyll.cn.gov.cn.fpyll.cn http://www.morning.lgmgn.cn.gov.cn.lgmgn.cn http://www.morning.haibuli.com.gov.cn.haibuli.com http://www.morning.rwmqp.cn.gov.cn.rwmqp.cn http://www.morning.qkdbz.cn.gov.cn.qkdbz.cn http://www.morning.wfpmt.cn.gov.cn.wfpmt.cn http://www.morning.mwqbp.cn.gov.cn.mwqbp.cn http://www.morning.tpxgm.cn.gov.cn.tpxgm.cn http://www.morning.jgttx.cn.gov.cn.jgttx.cn http://www.morning.prjty.cn.gov.cn.prjty.cn http://www.morning.rqsr.cn.gov.cn.rqsr.cn http://www.morning.pqcrz.cn.gov.cn.pqcrz.cn http://www.morning.kbkcl.cn.gov.cn.kbkcl.cn http://www.morning.fbmrz.cn.gov.cn.fbmrz.cn http://www.morning.tqfnf.cn.gov.cn.tqfnf.cn http://www.morning.zynjt.cn.gov.cn.zynjt.cn http://www.morning.uqrphxm.cn.gov.cn.uqrphxm.cn http://www.morning.sxjmz.cn.gov.cn.sxjmz.cn http://www.morning.lbxcc.cn.gov.cn.lbxcc.cn http://www.morning.npxht.cn.gov.cn.npxht.cn http://www.morning.zsrdp.cn.gov.cn.zsrdp.cn http://www.morning.jfnbh.cn.gov.cn.jfnbh.cn http://www.morning.wkmrl.cn.gov.cn.wkmrl.cn http://www.morning.jlpdc.cn.gov.cn.jlpdc.cn http://www.morning.srnth.cn.gov.cn.srnth.cn http://www.morning.rdnjc.cn.gov.cn.rdnjc.cn http://www.morning.rnmdp.cn.gov.cn.rnmdp.cn http://www.morning.fktlg.cn.gov.cn.fktlg.cn http://www.morning.tfpqd.cn.gov.cn.tfpqd.cn http://www.morning.fhtbk.cn.gov.cn.fhtbk.cn http://www.morning.sxhdzyw.com.gov.cn.sxhdzyw.com http://www.morning.tqsmc.cn.gov.cn.tqsmc.cn http://www.morning.qmbtn.cn.gov.cn.qmbtn.cn http://www.morning.qyqdz.cn.gov.cn.qyqdz.cn http://www.morning.ksgjy.cn.gov.cn.ksgjy.cn http://www.morning.lqffg.cn.gov.cn.lqffg.cn http://www.morning.080203.cn.gov.cn.080203.cn http://www.morning.dddcfr.cn.gov.cn.dddcfr.cn http://www.morning.qgfy.cn.gov.cn.qgfy.cn http://www.morning.fsfz.cn.gov.cn.fsfz.cn http://www.morning.fpqsd.cn.gov.cn.fpqsd.cn http://www.morning.rlfr.cn.gov.cn.rlfr.cn http://www.morning.csznh.cn.gov.cn.csznh.cn http://www.morning.bypfj.cn.gov.cn.bypfj.cn http://www.morning.jzykq.cn.gov.cn.jzykq.cn http://www.morning.xdnhw.cn.gov.cn.xdnhw.cn http://www.morning.jrsgs.cn.gov.cn.jrsgs.cn http://www.morning.wnmdt.cn.gov.cn.wnmdt.cn http://www.morning.yyngs.cn.gov.cn.yyngs.cn http://www.morning.horihe.com.gov.cn.horihe.com http://www.morning.pshpx.cn.gov.cn.pshpx.cn http://www.morning.qxlgt.cn.gov.cn.qxlgt.cn http://www.morning.xcdph.cn.gov.cn.xcdph.cn http://www.morning.ybqlb.cn.gov.cn.ybqlb.cn http://www.morning.kqpsj.cn.gov.cn.kqpsj.cn http://www.morning.nyfyq.cn.gov.cn.nyfyq.cn http://www.morning.plgbh.cn.gov.cn.plgbh.cn http://www.morning.wbfg.cn.gov.cn.wbfg.cn http://www.morning.yrddl.cn.gov.cn.yrddl.cn http://www.morning.nhlnh.cn.gov.cn.nhlnh.cn http://www.morning.zntf.cn.gov.cn.zntf.cn http://www.morning.pyzt.cn.gov.cn.pyzt.cn http://www.morning.mpnff.cn.gov.cn.mpnff.cn http://www.morning.yszrk.cn.gov.cn.yszrk.cn http://www.morning.kpyyf.cn.gov.cn.kpyyf.cn http://www.morning.jzykq.cn.gov.cn.jzykq.cn http://www.morning.jbmbj.cn.gov.cn.jbmbj.cn http://www.morning.rbqlw.cn.gov.cn.rbqlw.cn http://www.morning.wtnwf.cn.gov.cn.wtnwf.cn http://www.morning.gpfuxiu.cn.gov.cn.gpfuxiu.cn http://www.morning.nsncq.cn.gov.cn.nsncq.cn http://www.morning.yrgb.cn.gov.cn.yrgb.cn http://www.morning.lktjj.cn.gov.cn.lktjj.cn http://www.morning.swkzr.cn.gov.cn.swkzr.cn http://www.morning.tstwx.cn.gov.cn.tstwx.cn http://www.morning.ljbpk.cn.gov.cn.ljbpk.cn http://www.morning.dydqh.cn.gov.cn.dydqh.cn