Java StringBuffer类
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java StringBuffer类相关的知识,希望对你有一定的参考价值。
1 package demo03; 2 3 public class StringBufferDemo { 4 public static void main(String[] args) { 5 func(); 6 } 7 8 public static void func() { 9 StringBuffer buffer = new StringBuffer(); 10 buffer.append("abcd").append(1); 11 System.out.println(buffer); //abcd1 12 13 buffer.delete(1, 3); 14 System.out.println(buffer); //ad1 15 16 buffer.insert(1, "123"); 17 System.out.println(buffer); //a123d1 18 19 buffer.replace(1, 4, "jdk"); 20 System.out.println(buffer); //ajdkd1 21 22 buffer.reverse(); 23 System.out.println(buffer); //1dkdja 24 25 String s = buffer.toString(); 26 System.out.println(s); //1dkdja 27 28 } 29 }
demo
1 package demo03; 2 3 public class StringBufferTest { 4 //{1,2,3,4,5} => [1,2,3,4,5] 5 public static void main(String[] args) { 6 int[] arr = {1,2,3,4,5}; 7 System.out.println(toString(arr)); 8 } 9 10 public static String toString(int[] arr) { 11 StringBuffer buffer = new StringBuffer(); 12 buffer.append("["); 13 for (int i = 0; i < arr.length; i++) { 14 if (i == arr.length - 1) { 15 buffer.append(arr[i]).append("]"); 16 } else { 17 buffer.append(arr[i]).append(","); 18 } 19 } 20 21 return buffer.toString(); 22 } 23 24 }
以上是关于Java StringBuffer类的主要内容,如果未能解决你的问题,请参考以下文章
Java中的StringStringBuilder以及StringBuffer
Java api 入门教程 之 JAVA的StringBuffer类
Java学习(API及Object类String类StringBuffer字符串缓冲区)
Java—API/Obiect类的equals toString方法/String类/StringBuffer类/正则表达式