首页 > 其他 > 详细

String中重要方法与字段

时间:2015-10-22 09:12:53      阅读:180      评论:0      收藏:0      [点我收藏+]

下列这段代码已全部包含了我要写的String类中重要的字段:

 

//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 );
    }
}

以下是程序运行结果截图:

技术分享

 

String中重要方法与字段

原文:http://www.cnblogs.com/aishangtaxuefeihong/p/4899805.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!