java基础进阶-StringBuilder容器的基本用法
/*
- StringBuilder容器有.append(str)方法往容器里面添加字符,返回StringBuilder对象;
- 有.toString()方法转化成字符串,返回字符串对象;
- 有.reverse()方法实现反转,返回StringBuilder对象。
- 字符串转StringBuilder对象,用new StringBuilder(str)实现。
*/
上代码…public class ReverseWay {
public static void main(String[] args) {
String str = "abc";
//转换成StringBulider对象
StringBuilder sb = new StringBuilder();
sb.append(str);
sb.append("helloworld!");
System.out.println(sb);
System.out.println(sb.charAt(0));
System.out.println(sb.length());
//匿名对象调用方法
int len = new StringBuilder(str).append("helloworld!").length();
System.out.println(len);
//反转字符串方式输出
String sc = new StringBuilder(str).append("helloworld!").reverse().toString();
System.out.println(sc);
}
}
还没有评论,来说两句吧...