Java基础:字符串String

示例代码:

字符串操作方法:

  1. class StringOps{
  2.     public static void main(String args[]){
  3.         String str1 = "hello java is important for u";
  4.         String str2 = new String(str1);
  5.         String str3 = "hello one two three four one";
  6.         int dix,result;
  7.         
  8.         if(str1.equals(str2)){                      //boolean equals(str) 相等返回true
  9.             System.out.println("str1 equals str2");
  10.             }else{
  11.                 System.out.println("str1 not equals str2");
  12.                 }
  13.         
  14.         System.out.println("str1的长度是:"+ str1.length());    //int length()返回字符串长度
  15.     
  16.         for(int i=0;i < str2.length();i++){
  17.             System.out.print(str2.charAt(i));   //char charAt(index) 获取index位置的字符
  18.             }
  19.         System.out.println();
  20.         
  21.         result = str1.compareTo(str3);               //int compareTo(str) 小于返回小于0的数,等于返回0,大于返回大于0的数
  22.         if(result == 0){
  23.                 System.out.println("str1 与 str3相等");
  24.             }else if(result < 0){
  25.                 System.out.println("str1 比 str3小");;
  26.                 }else{
  27.                     System.out.println("str1 比 str3大");;
  28.                     }
  29.                     
  30.         dix = str3.indexOf("one");         //int indexOf(str) 查找字符串,返回第一个匹配的索引,没有匹配返回-1
  31.         System.out.println("the first one is:"+ dix);
  32.         dix = str3.lastIndexOf("one");  //int lastIndexOf(str)查找字符串,返回最后一个匹配的索引,没有返回-1
  33.         System.out.println("the last one is:"+ dix);
  34.         }
  35.     }

-------------------------------------------------------------------------------------------------------------

字符串数组:

  1. class StringArrays{
  2.     public static void main(String args[]){
  3.         String[] str1 = {"hello", "java", "is", "important", "for", "u"};  //字符串数组
  4.         System.out.println("origial:");
  5.         for(String s : str1){                //for-each循环输出内容
  6.             System.out.print(s+" ");
  7.             }
  8.         System.out.println();
  9.         str1[2] = "was";              //修改元素
  10.         str1[5] = "me";
  11.         System.out.println("modified:");
  12.         for(String s : str1){
  13.             System.out.print(s+" ");
  14.             }
  15.         System.out.println();
  16.  
  17.         String str2 = str1.substring(6,20);       //substring(int startindex,int endindex)返回指定部分字符串
  18.         System.out.println(str1);        
  19.         System.out.println(str2);        
  20.  
  21.         }
  22.     }

------------------------------------------------------------------------------------------------------------------

用String控制switch语句:

  1. class StringSwitch{
  2.     public static void main(String args[]){
  3.         String command = "cancel";
  4.         switch(command){                         //switch()语句
  5.             case "connect":{
  6.                 System.out.println("Connecting");
  7.                 break;
  8.                 }
  9.             case "cancel":{
  10.                 System.out.println("Canceling");
  11.                 break;
  12.                 }
  13.             case "disconnect":{
  14.             System.out.println("DisConnecting");
  15.             break;
  16.             }
  17.             default:{
  18.                 System.out.println("command error!");
  19.                 break;
  20.                 }
  21.             }
  22.         }
  23.     }
  24.  

-----------------------------------------------------------------------------

使用命令行实参:命令行执行java CLDemo one two three

  1. class CLDemo{
  2.     public static void main(String args[]){
  3.     System.out.println(args.length);
  4.     
  5.     for(int i=0;i < args.length;i++){
  6.         System.out.println(args[i]);
  7.         } 
  8.      }  
  9.  }

猜你喜欢

转载自blog.csdn.net/QQhelphelp/article/details/87926190