StringBuilder 拼接字符串
package com.itheima;
public class one_131 {
public static void main(String[] args) {
int[] arr = {1,2,3};
System.out.println(pinjie(arr));
}
public static String pinjie(int[] arr){
StringBuilder s1 = new StringBuilder();
s1.append("[");
for(int i=0;i<arr.length;i++){
if(i == arr.length-1){
s1.append(arr[i]);
}else{
s1.append(arr[i]).append(",");
}
}
s1.append("]");
String s2 = s1.toString();
return s2;
}
}
StringBuilder 字符串反转
package com.itheima;
import java.util.Scanner;
public class one_132 {
public static void main(String[] args) {
System.out.println(“请输入一个字符串:”);
Scanner s1 = new Scanner(System.in);
String s2 = s1.nextLine();
InReverse(s2);
System.out.println(InReverse(s2));
}
public static String InReverse(String a){
StringBuilder b = new StringBuilder(a);
b.reverse();
String c = b.toString();
return c;
//return new StringBuilder(a).reverse().toString();
}
}