StringBuilder

public class SB {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder();
sb.append("Hello").append("Word").append("Java"); //链式编程,返回的是对象本身所以可以继续调方法
System.out.println(sb); //append 方法 和 reverse 方法

System.out.println(sb.reverse()); //反转字符串方法 reverse


sb.append("Java");
String s = sb.toString(); //StringBuilder 转为 String 通过toString方法
System.out.println(s);

String s1 = "Hello ";
StringBuilder sb1 = new StringBuilder(s1); //String 转为 StringBuilder 通过构造方法
System.out.println(sb1);


int[] array = {1, 2, 3};
String result = PJ(array); //调用拼接字符串方法
System.out.println(result);


Scanner scanner = new Scanner(System.in);
String line = scanner.nextLine();
String result1 = reverse(line);
System.out.println(result1);
}

public static String PJ(int[] arr){ //使用StringBuilder拼接字符串
StringBuilder sb = new StringBuilder();
sb.append("[");
for (int i = 0; i < arr.length; i++) {
if (i == arr.length-1){
sb.append(arr[i]);
}else {
sb.append(arr[i]+",");
}
}
sb.append("]");
String s = sb.toString();
return s;
}
public static String reverse(String s){ //使用StringBuilder反转字符串
// StringBuilder ss = new StringBuilder(s);
// ss.reverse();
// String j = ss.toString();
// return j;

return new StringBuilder().reverse().toString();
}
}
上一篇:(联考)noip90


下一篇:夺旗赛 CTF 六大方向基础工具简介集合(MISC,WEB,Crypto,Reverse,Pwn,Mobile)