什么情况下用+运算符进行字符串连接比调用StringBuffer/StringBuilder对象的append方法连接字符串性能更好?

如果在编写代码的过程中大量使用+进行字符串评价还是会对性能造成比较大的影响,但是使用的个数在1000以下还是可以接受的,大于10000的话,执行时间将可能超过1s,会对性能产生较大影响。如果有大量需要进行字符串拼接的操作,最好还是使用StringBufferStringBuilder进行。

String类的常用用法:


public class StrTest {/*** 字符串练习* @param args*/public static void main(String[] args) {String str=new String(" WOrld ");String str1=new String("w,o,r;l;d");System.out.println(str.length());//获取长度System.out.println(str.toUpperCase());//转为大写System.out.println(str.toLowerCase());//转为小写System.out.println(str.trim());//去掉首尾的空格
        System.out.println(str.trim().length());System.out.println(str.equals(str1));//比较是否相等,区分大小写System.out.println((str.trim()).equalsIgnoreCase(str1));//不区分大小写//查找System.out.println(str.indexOf("or"));//返回第一次出现这个字符的位置,如果没有找到返回-1System.out.println(str.lastIndexOf('l'));//返回最后一次出现这个字符的位置System.out.println(str.lastIndexOf("lp",4));//从字符串第四个索引位置开始找lp最后一次出现的位置System.out.println(str1.charAt(3));//返回索引位置为3的字符//截取System.out.println(str.substring(2, 4));//截取索引位置为2-4之间的字符//分割String[] array1=str1.split(",");String[] array2=str1.split(";",3);//分割;标志的字符串,指定分割字符数为3,返回的字符存放在字符串数组中for(int i=0;i){System.out.print(array1[i]+"\t");}System.out.println();for(int i=0;i){System.out.print(array2[i]+"\t");}//替换String s="hello world,hello world";System.out.println(s.replace("world", "java"));//用java替换所有的worldSystem.out.println(s.replaceFirst("world", "java"));//用java只替换第一次出现的worldSystem.out.println(s.replaceAll("world", "java"));//用将咪表字符串匹配的正则表达式的所有子字符串替换成新的//链接System.out.println(s.concat(str1));//在s字符串后链接str1字符串
}
}
复制代码

结果显示:

复制代码
7WORLD world 
WOrld
5
false
false
-1
4
-1
,
Or
w    o    r;l;d    
w,o,r    l    d    hello java,hello java
hello java,hello world
hello java,hello java
hello world,hello worldw,o,r;l;d
复制代码


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部