JAVA基礎知識點(九)--String類

一、String

筆試題目:new String("abc")創建了幾個對象?

兩個對象,一個對象是位於字符串常量池中,一個對象是位於堆內存中。

 

String 的構造方法:

        String()  創建一個空內容的字符串對象。

        String(byte[] bytes)  使用一個字節數組構建一個字符串對象

        String(byte[] bytes, int offset, int length)

                 bytes :  要解碼的數組

                offset:指定從數組中那個索引值開始解碼。

                length: 要解碼多少個元素。

        String(char[] value)  使用一個字符數組構建一個字符串。  

        String(char[] value, int offset, int count)  使用一個字符數組構建一個字符串

         String(int[] codePoints,int offset,int count)

         String(String original)

記住:使用字節數組或者字符數組都可以構建一個字符串對象。

 

2.2   獲取方法

         int length()  獲取字符串的長度

         char charAt(int index) 獲取特定位置的字符 (角標越界)

         int indexOf(String str) 查找子串第一次出現的索引值,如果子串沒有出現在字符串中,那麼則返回-1表示。

       int lastIndexOf(String str) 查找子串最後一次出現的索引值 , 如果子串沒有出現在字符串中,那麼則返回-1表示

public class Demo3 {

         public static void main(String[] args) {

                  String str = "abc中國ab中國";

                  System.out.println("字符串的字符個數: " + str.length() );

                  System.out.println("根據索引值獲取對應的字符:"+ str.charAt(3));

                  System.out.println("查找子串第一次出現的索引值:" + str.indexOf("中國"));

                  System.out.println("查找子串最後一次出現的索引值:" + str.lastIndexOf("中國"));

         }

}

 

 

2.3   判斷方法

         boolean endsWith(String str) 是否以指定字符結束

         boolean isEmpty()是否長度爲0 如:“” null V1.6

         boolean contains(CharSequences) 是否包含指定序列應用:搜索

         boolean equals(Object anObject) 是否相等

         boolean equalsIgnoreCase(String anotherString) 忽略大小寫是否相等

 

2.4   轉換方法    

char[] toCharArray()  將字符串轉換爲字符數組

byte[]getBytes();

字節數組與字符數組、字符串他們三者之間是可以互相轉換。

public class Demo4 {

        

         public static void main(String[] args) {

                  String str = "Demo.java";

                  System.out.println("是否以指定的字符串結束:"+ str.endsWith("ja"));

                  //str = "";

                  System.out.println("判斷字符串是否爲空內容:"+str.isEmpty());

                  System.out.println("判斷字符串是否包含指定的內容:"+ str.contains("Demo"));

                  System.out.println("判斷兩個字符串的內容是否一致:"+ "DEMO.JAVA".equals(str));

                  System.out.println("判斷兩個字符串的內容是否一致(忽略大小寫比較):"+ "DEMO.JAVA".equalsIgnoreCase(str));

                 

                  //轉換的方法

                  char[] buf = str.toCharArray();  //把字符串轉換字符數組

                  System.out.println("字符數組:"+ Arrays.toString(buf));

                  byte[] buf2 = str.getBytes();   //把字符串轉字節數組

                  System.out.println("字節數組:"+ Arrays.toString(buf2));

         }

}

 

其他方法

         String replace(String oldChar, String newChar) 替換

         String[] split(String regex) 切割

        

         String substring(int beginIndex)   指定開始的索引值截取子串

         String substring(int beginIndex, int endIndex)指定開始與結束的索引值截取子串

         String toUpperCase() 轉大寫

         String toLowerCase() 轉小寫

         String trim() 去除字符串首尾的空格

 

public class Demo5 {

         public static void main(String[] args) {

                  String str = "今天晚上不考試";

                  System.out.println("指定新內容替換舊的內容:"+ str.replace("", "要好好"));

                  str = "大家---";

                  String[] arr = str.split("-"); //根據指定的字符進行切割。

                  System.out.println("字符串數組的內容:"+ Arrays.toString(arr));

                  str = "廣州傳智播客";

                  System.out.println("指定開始的索引值截取子串:"+ str.substring(2));

                  System.out.println("指定開始的索引值截取子串:"+ str.substring(2,6)); //包頭不包尾  //注意:截取的內容是包括開始的索引值,不包括結束的索引值,截取的位置是結束的索引值-1.

                  str = "abC中國";

                  System.out.println("轉大寫:" + str.toUpperCase());

                  str = "AbdfSDD";

                  System.out.println("轉小寫:"+ str.toLowerCase());

                  str = "         大家最近        都非常努力            ";

                  System.out.println("去除字符串首尾的空格:"+ str.trim());

         }

}

練習:

package cn.itcsat.string;
/*
需求1:自己實現trim的方法(去除字符串收尾的空格)。

需求2: 獲取上傳文件名  "D:\\20120512\\day12\\Demo1.java"。

需求3: 將字符串對象中存儲的字符反序。    新中國好     -----> 好國中新

需求4: 求一個子串在整串中出現的次數 。
 

*/
public class Demo6 {
 
 public static void main(String[] args) {
  String str  ="        傳智        播客             "; 
  System.out.println(myTrim(str));
  
  str =  "D:\\20120512\\day12\\Demo1.java";
  getFileName(str);
  
  str = "新中國好";
  System.out.println("翻轉後的字符串:"+ reverse(str));
  
  str = "abcjavaabcjavaphpjava";  //java
  getCount(str, "java");
  
 }
 
 
 //統計子串出現 的次數
 public static void getCount(String str,String target){
  int count = 0 ; //用於記錄出現的次數
  int fromIndex  = 0; // 記錄從那個索引值開始尋找目標子串
  while((fromIndex = str.indexOf(target, fromIndex))!=-1){
   //如果indexof方法返回 的不是-1,那麼就是已經找到了目標 元素。
   count++;
   fromIndex = fromIndex+target.length();
  }
  System.out.println("出現的次數:"+ count);
 }
 
 
 
 public static String reverse(String str){
  char[] arr = str.toCharArray();
  for(int startIndex = 0 , endIndex=arr.length-1 ; startIndex<endIndex; startIndex++,endIndex--){
    char temp = arr[startIndex];
    arr[startIndex] = arr[endIndex];
    arr[endIndex] = temp;
  }
  //使用字符數組構建一個字符串。
  return new String(arr);
 }
 
 
 
 
 
 //需求2: 獲取上傳文件名  "D:\\20120512\\day12\\Demo1.java"。
 public static void getFileName(String path){
  int index = path.lastIndexOf("\\");
  String fileName = path.substring(index+1);
  System.out.println("文件名:"+ fileName);
 }
 
 
 
// 需求1:自己實現trim的方法。
 public static String myTrim(String str){
  //先轉換成字符 數組
  char[] arr = str.toCharArray();
  //定義兩個 變量記錄開始與結束 的索引值
  int startIndex = 0 ;
  int endIndex = arr.length -1;
  //確定開始 的索引值
  while(true){
   if(arr[startIndex]==' '){
    startIndex++;
   }else{
    break;
   }
  }
  //確定結束 的索引值:
  while(true){
   if(arr[endIndex]==' '){
    endIndex--;
   }else{
    break;
   }
  }
  //截取子串返回
  return str.substring(startIndex,endIndex+1);  
 }
 

}

 

發佈了49 篇原創文章 · 獲贊 0 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章