集册 Java实例教程 演示String类的长度,charAt和getChars方法。

演示String类的长度,charAt和getChars方法。

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

434
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
演示String类的长度,charAt和getChars方法。

public class Main 
/**来自 
 时 代 J a v a 公 众 号 - N o w J a v  a . c o m**/

{

   public static void main(String[] args)

   {

      String s1 = "hello there";

      char[] charArray = new char[5];


      System.out.printf("s1: %s", s1);


      // test length method

      System.out.printf("\nLength of s1: %d", s1.length());


      // loop through characters in s1 with charAt and display reversed

      System.out.printf("%nThe string reversed is: ");/** 来 自 时 代 J a v a**/


      for (int count = s1.length() - 1; count >= 0; count--)

         System.out.printf("%c ", s1.charAt(count));


      // copy characters from string into charArray

      s1.getChars(0, 5, charArray, 0);

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