集册 Java实例教程 字符串方法替换,toLowerCase,toUpperCase,trim和toCharArray。

字符串方法替换,toLowerCase,toUpperCase,trim和toCharArray。

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

520
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
字符串方法替换,toLowerCase,toUpperCase,trim和toCharArray。

public class Main 

{

   public static void main(String[] args)

   {//来 自 时代Java - N o w  J a v a . c o m

      String s1 = "hello";

      String s2 = "GOODBYE";

      String s3 = "   spaces   ";


      System.out.printf("s1 = %s\ns2 = %s\ns3 = %s\n\n", s1, s2, s3);


      // test method replace      

      System.out.printf(

         "Replace 'l' with 'L' in s1: %s\n\n", s1.replace('l', 'L'));


      // test toLowerCase and toUpperCase

      System.out.printf("s1.toUpperCase() = %s\n", s1.toUpperCase());

      System.out.printf("s2.toLowerCase() = %s\n\n", s2.toLowerCase());


      // test trim method

      System.out.printf("s3 after trim = \"%s\"\n\n", s3.trim());
/** 来 自 N o w  J a v a  .   c o m**/

      // test toCharArray method

      char[] charArray = s1.toCharArray();

      
展开阅读全文