문자열 반전의 몇 가지 방법
1225 단어 JavaSE 기반
public static String reverse(String original){
if(original==null|| original.length()<=1)
return original;
return reverse(original.substring(1))+original.charAt(0);
}
2. String의 몇 가지 방법
public class test{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print(" :");
String s1 = input.nextLine();//
System.out.print(" :");
for(int i=s1.length()-1;i>=0;i--){
//charAt() char 。 0 length() - 1
System.out.print(s1.charAt(i));
}
}
}
import java.util.*;
public class StringChange{
public static void main(String[] args){
System.out.println("Please enter the String:");
String str = new Scanner(System.in).nextLine(); //
String s2[] = str.split("\\s"); // \s , s2
for (int i = s2.length-1; i >= 0; i--) { //
System.out.print(s2[i]+" ");
}
}
}
3. StringBuffer의 내장 리버스 방법 활용
public static String reverse2(String s){
String str;
StringBuffer sbBuffer =new StringBuffer(s);
str=sbBuffer.reverse().toString();
return str;
}