StringBuilder和StringBuffer的append
其示例代码如下所示:
public class StringTest {public static void main(String[] args) throws IOException {// 可以拼接所有的基本数据类型StringBuilder strBuilder = new StringBuilder();StringBuffer strBuffer = new StringBuffer();// 拼接int(byte、short都可以自动转换为int)strBuilder.append(12).append(",");strBuffer.append(12).append(",");// 拼接longstrBuilder.append(13L).append(",");strBuffer.append(13L).append(",");// 拼接floatstrBuilder.append(3.4f).append(",");strBuffer.append(3.4f).append(",");// 拼接doublestrBuilder.append(3.5).append(",");strBuffer.append(3.5).append(",");// 拼接字符数组strBuilder.append("hello".toCharArray()).append(",");strBuffer.append("hello".toCharArray()).append(",");// 拼接其他引用对象strBuilder.append(new Date()).append(",");strBuffer.append(new Date()).append(",");// 拼接指定字符数组偏移指定位数后的指定长度字符strBuilder.append("hello".toCharArray(), 2, 2).append(",");strBuffer.append("hello".toCharArray(), 2, 2).append(",");// 拼接指定字符序列对象(常见的为String、StringBuffer和StringBuilder)指定开始和结束(不包括)的字符串strBuilder.append("hello", 1, 3).append(",");strBuffer.append("hello", 1, 3).append(",");printStrBuilder(strBuilder);printStrBuffer(strBuffer);}private static void printStrBuilder(StringBuilder strBuilder) {String[] strArr = strBuilder.deleteCharAt(strBuilder.length() - 1).toString().split(",");System.out.println("StringBuilder信息为:n"Arrays.asList(strArr));}private static void printStrBuffer(StringBuffer strBuffer) {String[] strArr = strBuffer.deleteCharAt(strBuffer.length() - 1).toString().split(",");System.out.println("StringBuffer信息为:n"Arrays.asList(strArr));}} 只想结果如下图所示:
获取某个字符串在另一个字符串中的索引位置这里使用的方法有四个,如下图:
相关的示例代码如下所示:
public class StringTest {public static void main(String[] args) throws IOException {StringBuilder strBuilder = new StringBuilder("no zuo no die no happy no problem");StringBuffer strBuffer = new StringBuffer("no zuo no die no happy no problem");// indexOfSystem.out.println(""no"在strBuilder中首次出现的位置为:"strBuilder.indexOf("no"));System.out.println(""no"在strBuffer中首次出现的位置为:"strBuffer.indexOf("no"));System.out.println(""no"在strBuilder中在索引3之后首次出现的位置为:"strBuilder.indexOf("no", 3));System.out.println(""no"在strBuffer中在索引3之后首次出现的位置为:"strBuffer.indexOf("no", 3));// lastIndexOfSystem.out.println(""no"在strBuilder中最后出现的位置为:"strBuilder.lastIndexOf("no"));System.out.println(""no"在strBuffer中最后出现的位置为:"strBuffer.lastIndexOf("no"));System.out.println(""no"在strBuilder中在索引20之前最后出现的位置为:"strBuilder.lastIndexOf("no", 20));System.out.println(""no"在strBuffer中在索引20之前最后出现的位置为:"strBuffer.lastIndexOf("no", 20));}}
猜你喜欢
- 梦见轶闻是什么意思 梦见轶闻梦境解析
- 亲爱的自己刘洋离婚了吗 亲爱的自己刘洋离婚原因解析
- 全款买房后还能办公积金贷款吗?怎么办 全款买房后是否能办公积金贷款解析
- 什么时候长智齿 什么时候长智齿的解析
- 家常菜是什么意思?
- 牛油果变色了还能吃吗 牛油果变色是否能吃解析
- 网络推广怎么挣钱,网络推广工作流程
- wav和flac音质有区别吗 wav和flac音质有没有区别
- 移动和电信宽带哪个好,,求解析。。。。。
- 吃完柿子可以喝酸奶吗 吃完柿子是否可以喝酸奶的解析
