String的幾個常用API


一.  indexOf 用於實現檢索

 1 /**
 2  * indexOf
 3  * 檢索字符串位置
 4  * (查找給定字符串在當前字符串的位置,返回第一個字母所在下標)
 5  * @author Administrator
 6  *
 7  */
 8 public class StringDemo {
 9     public static void main(String[] args){
10         String str = "thinking in Java";
11         
12         int index = str.indexOf("in");
13         System.out.println("index="+index);//index=2
14         
15         int index1 = str.indexOf("in",3);//從第4個字符開始尋找in
16         System.out.println("index1="+index1); //index1=5
17         
18         int index2 = str.lastIndexOf("in");//從后往前查找
19         System.out.println("index2="+index2);//index2=9
20     }
21 }

 

二.  substring獲取子串位置

 1 /**
 2  * substring(int start,int end)方法用於返回一個字符串的子字符串
 3  * 截取當前字符串的部分內容
 4  * @author Administrator
 5  *
 6  */
 7 public class StringDemo03 {
 8     public static void main(String[] args) {
 9         //              01234567890123 
10         String str = "www.oracle.com";
11         /*
12          * 使用數字表示范圍,都是含頭不含尾,
13          * 例如從4開始,到10結束
14          */
15          String sub = str.substring(4,10);
16          System.out.println(sub);//oracle
17          /*
18           * 只有一個數字表示從這開始一直到結束
19           */
20          String sub1 = str.substring(4);
21          System.out.println(sub1);//oracle.com
22          
23          /**
24           * 獲取www.oracle.com的域名
25           * 
26           */
27          int start = str.indexOf(".");
28          //int end = str.lastIndexOf(".");
29          int end = str.indexOf(".",start+1);
30          sub = str.substring(start+1,end);
31          System.out.println("域名:"+sub);
32     }
33 }

 

三.  trim去除字符串周圍空白

 1 /**
 2  * trim去除字符串兩邊的空白
 3  * @author Administrator
 4  *
 5  */
 6 public class StringDemo02 {
 7     public static void main(String[] args) {
 8         String str = "    Hello World         ";
 9         String trim = str.trim();
10         System.out.println(trim);
11         }
12 }
13     

 

四.  charAt 用於給出位置上的字符

 1 /**
 2  * charAt 用於給定位置上的字符
 3  * char charAt(int index)
 4  * @author Administrator
 5  *
 6  */
 7 public class StringDemo04 {
 8     public static void main(String[] args) {
 9         String str = "beautiful";
10         char a = str.charAt(5);
11         System.out.println(a);
12         
13         /**
14          * 判斷回文
15          */
16         str = "霧鎖山頭山鎖霧";
17         for(int i = 0;i<str.length()/2;i++){
18             if(str.charAt(i) != str.charAt(str.length()-1-i)){
19                 System.out.println("不是回文");
20                 return;
21             }
22             
23         }System.out.println("是回文");
24     }
25 }

 

五.  startsWith和endsWith用於判斷是否以指定字符開頭或結尾

 1 /**
 2  * 判斷當前字符串是否以給定的字符串開頭或結尾的
 3  * boolean startsWith(String str)
 4  * boolean endsWith(String str)
 5  * @author Administrator
 6  *
 7  */
 8 public class StringDemo05 {
 9     public static void main(String[] args) {
10         String str = "Everything will be good";
11         boolean a = str.startsWith("Eve");
12         System.out.println(a);//true
13         
14         boolean b = str.endsWith("d");
15         System.out.println(b);//true
16     }
17 }

六.  String toUpperCase() 將指定字符串全部轉換為大寫

   String toLowerCase() 將指定字符串全部轉換為小寫

七.  valueOf 用於將其他類型轉換為字符串類型

 1 /**
 2  * valueOf()將基本類型轉換為字符串
 3  * @author Administrator
 4  *
 5  */
 6 public class StringDemo06 {
 7     public static void main(String[] args) {
 8         int i = 10;
 9         double a = 0.1;
10         double b = 0.2;
11         String j = String.valueOf(i);//已經轉換為字符串了
12         /*
13          * 運行結果是100.1;因為是字符串相加
14          */
15         System.out.println(j+a);
16         System.out.println(j+a+b);//100.10.2
17         
18         /*
19          * 后面加上“”可以可以轉換為字符串類型
20          */
21         String str = 10+"";
22         System.out.println(str+a);//100.1 
23     
24     }
25 }

 

   


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM