集册 Java实例教程 StringBuilder方法charAt,setCharAt,getChars和reverse。

StringBuilder方法charAt,setCharAt,getChars和reverse。

欢马劈雪     最近更新时间:2020-01-02 10:19:05

605
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
StringBuilder方法charAt,setCharAt,getChars和reverse。

public class Main // 来 自 n o w j a v a . c o m

{

   public static void main(String[] args)

   {

      StringBuilder buffer = new StringBuilder("hello there");


      System.out.printf("buffer = %s\n", buffer.toString());

       System.out.printf("Character at 0: %s\nCharacter at 4: %s\n\n", 

         buffer.charAt(0), buffer.charAt(4));


      char[] charArray = new char[buffer.length()];

      buffer.getChars(0, buffer.length(), charArray, 0);

      System.out.print("The characters are: ");


      for (char character : charArray)

         System.out.print(character);


      buffer.setCharAt(0, 'H');

      buffer.setCharAt(6, 'T');

      System.out.printf(
展开阅读全文