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

📄 stringmisc.java

📁 JAVA分布式程序学习的课件(全英文)
💻 JAVA
字号:

// Deitel Fig. 8.9: StringMisc2.java
// This program demonstrates the String class replace,
// toLowerCase, toUpperCase, trim, toString, and toCharArray
// methods.
import java.io.*;

class StringMisc {

   
   public static void main(String[] args) throws IOException{
     String s1 = new String( "hello" ),
            s2 = new String( "GOOD BYE" ),
            s3 = new String( "   spaces   " );

      System.out.println ("Miscellaneous string methods\n");

      System.out.println( "s1 = " + s1 );

      System.out.println( "s2 = " + s2 );

      System.out.println( "s3 = " + s3 );

      // test method replace      
      System.out.println( "Replace 'l' with 'L' in s1: " +
                    s1.replace( 'l', 'L' ) );

      // test toLowerCase and toUpperCase
      System.out.println( "s1 after toUpperCase = " + 
                    s1.toUpperCase() );

      System.out.println( "s2 after toLowerCase = " +
                    s2.toLowerCase() );

      // test trim method
      System.out.println( "s3 after trim = \"" + s3.trim() + "\"");

      // test toString method
      System.out.println( "s1 = " + s1.toString() );

      // test toCharArray method
      char charArray[] = s1.toCharArray();
      System.out.println( "s1 as a character array = " );
      System.out.println( charArray );

      // use .equals to compare to strings, NOT ==
      if (s1.equals(s2))
        System.out.println("s1 and s2 contain the same character string");
   }
}

⌨️ 快捷键说明

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