字符串反转charAt

public class Fanzhuan{
  
  public static void main(String[] args){
    String str = "xiaoxin";
    System.out.print("xiaoxin的逆转的字符串:");
    for(int i=str.length()-1;i>=0;i--){
      char ch = str.charAt(i);
      System.out.print(ch);
    }
//charAt(int index)方法是一个能够用来检索特定索引下的字符的String实例的方法.

//charAt()方法返回指定索引位置的char值。索引范围为0~length()-1.

  //II用字符串反转的命令
  /×String str = "good";
  StringBuffer sb = new StringBuffer(str);
  System.out.print("逆转后的字符串:"+sb.reverse());×/

  //III
  /*String str = "xiaoxin";
  char s[] = str.toCharArray();//转换成数组
  System.out.print("逆转的字符串:");
  for( int i=s.length-1;i>=0;i--){
    System.out.pring(s[i]);
  }*/
  }
}

猜你喜欢

转载自ztao2333.iteye.com/blog/2272207