package stringdemo;
public class StringTest {
public static void main(String[] args) throws InterruptedException {
long begin = System.currentTimeMillis();
String s = "";
for (int i = 0; i < 1000000; i++) {
s = s + "abc";
}
System.out.println(s);
long end = System.currentTimeMillis();
System.out.println(String.valueOf(end - begin));
}
}
1.2、StringBuilder 拼接100万次
java复制代码
package stringdemo;
public class StringTest {
public static void main(String[] args) throws InterruptedException {
long begin = System.currentTimeMillis();
StringBuilder sb = new StringBuilder("");
for (int i = 0; i < 1000000; i++) {
sb.append("abc");
}
System.out.println(sb);
long end = System.currentTimeMillis();
System.out.println(String.valueOf(end - begin));
}
}
2、StringBuilder概述
StringBuilder可以看成是一个 容器 ,创建之后里面的 内容是可变的
2.1、作用
提高字符串的操作效率
2.2、StringBuilder构造方法
java在底层对它做了一些特殊处理,打印对象不是地址值而是 属性值 。
方法名
说明
* public StringBuilder()
* 创建一个空白可变字符串对象,不含有任何内容
* public StringBuilder(String str)
* 根据字符串的内容,来创建可变字符串对象
2.3、StringBuilder常用方法
方法名
说明
* public StringBuilder append(任意类型)
* 添加数据,并返回对象本身
* public StringBuilder reverse()
* 反转容器中的内容
* public int length()
* 返回长度(字符出现的个数)
* public String toString()
* 通过toString()就可以实现把StringBuilder转换为String
java复制代码
package stringdemo;
public class StringBuilderTest {
public static void main(String[] args) throws InterruptedException {
StringBuffer sb = new StringBuffer("abc");
sb.append(1);
sb.append(2.3);
sb.append(true);
System.out.println(sb);//abc12.3true
sb.reverse();
System.out.println(sb);//eurt3.21cba
int length = sb.length();
System.out.println(length);//11
String string = sb.toString();
System.out.println(string);//eurt3.21cba
}
}
D:\Java\jdk-17\bin\java.exe "-javaagent:D:\BaiduNetdiskDownload\IntelliJ IDEA 2023.2\lib\idea_rt.jar=2315:D:\BaiduNetdiskDownload\IntelliJ IDEA 2023.2\bin" -Dfile.encoding=UTF-8 -classpath F:\IdeaProjects\JavaSenior\out\production\Exception stringdemo.StringBuilderTest
abc12.3true
eurt3.21cba
11
eurt3.21cba
Process finished with exit code 0
2.4、链式编程
当我们在调用一个方法的时候,不需要用变量接收他的结果,可以继续调用其他方法
java复制代码
package stringdemo;
public class StringBuilderTest {
public static void main(String[] args) throws InterruptedException {
StringBuffer sb = new StringBuffer();
sb.append("aaa").append("bbb").append("ccc").append("ddd");
System.out.println(sb);
String str = sb.toString();
System.out.println(str);
}
}
D:\Java\jdk-17\bin\java.exe "-javaagent:D:\BaiduNetdiskDownload\IntelliJ IDEA 2023.2\lib\idea_rt.jar=23088:D:\BaiduNetdiskDownload\IntelliJ IDEA 2023.2\bin" -Dfile.encoding=UTF-8 -classpath F:\IdeaProjects\JavaSenior\out\production\Exception stringdemo.StringBuilderTest
aaabbbcccddd
aaabbbcccddd
Process finished with exit code 0
3、使用StringBuilder的场景
字符串的 拼接
字符串的 反转
4、练习-对称字符串
需求:键盘接受一个字符串,程序判断出该字符串是否是对称字符串,并在控制台打印是或不是
java复制代码
package stringdemo;
import java.util.Scanner;
public class StringBuilderTest {
public static void main(String[] args) throws InterruptedException {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个字符串");
String str = sc.next();
StringBuilder sb = new StringBuilder();
sb.append(str);
sb.reverse();
String string = sb.toString();
System.out.println(string);
if (str.equals(string)) {
System.out.println("当前字符串是对称字符串");
} else {
System.out.println("当前字符串不是对称字符串");
}
}
}
D:\Java\jdk-17\bin\java.exe "-javaagent:D:\BaiduNetdiskDownload\IntelliJ IDEA 2023.2\lib\idea_rt.jar=2263:D:\BaiduNetdiskDownload\IntelliJ IDEA 2023.2\bin" -Dfile.encoding=UTF-8 -classpath F:\IdeaProjects\JavaSenior\out\production\Exception stringdemo.StringBuilderTest
请输入一个字符串
123321
123321
当前字符串是对称字符串
Process finished with exit code 0