⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stringmisc.java

📁 金旭亮的java教案
💻 JAVA
字号:
//StringMisc.java
// This program demonstrates the length, charAt and getChars
// methods of the String class.
//
// Note: Method getChars requires a starting point
// and ending point in the String. The starting point is the
// actual subscript from which copying starts. The ending point
// is one past the subscript at which the copying ends.
import javax.swing.*;

public class StringMisc {
   public static void main( String args[] )
   {
      String s1, output;
      char charArray[];

      s1 = new String( "hello there" );
      charArray = new char[ 5 ];

      // output the string
      output = "s1: " + s1;

      // test the length method
      output += "\nLength of s1: " + s1.length();

      // loop through the characters in s1 and display reversed
      output += "\nThe string reversed is: ";

      for ( int i = s1.length() - 1; i >= 0; i-- )
         output += s1.charAt( i ) + " ";

      // copy characters from string into char array
      //四个参数的含义
      //1.被拷贝字符在字串中的起始位置
      //2.被拷贝的最后一个字符在字串中的下标再加1
      //3.目标字符数组
      //4.拷贝的字符放在字符数组中的起始下标
      s1.getChars( 0, 5, charArray, 0 );
      output += "\nThe character array is: ";

      for ( int i = 0; i < charArray.length;i++ )
         output += charArray[ i ];

      JOptionPane.showMessageDialog( null, output,
         "Demonstrating String Class Constructors",
         JOptionPane.INFORMATION_MESSAGE );

      System.exit( 0 );
   }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -