27 String類

27 String類

底層實現

​ final的char數組

常用的加強版

StringBuffer

特點:線程安全、速度慢

StringBuilder

特點:線程不安全、速度快

出現的原因:解決String自行拼接以及反序麻煩等問題

加強版與String類型的轉換

對象名.toString();------------//大部分類型的數據轉String類的方法都是toString()方法

StringBuffer stringBuffer=new StringBuffer();
String str=stringBuffer.toString();

String的常用方法

前提:

對字符串進行任意修改,返回的是一個新字符串,原來的字符串保持不變

若是對字符串進行判斷,則返回的是布爾類型

1.根據下標找對應的值

字符串名稱.indexof(int ch);
//返回指定字符在此字符串中第一次出現的索引
字符串名稱.indexof(int ch,fromIndex);
//從指定的索引起,返回指定字符在此字符串中第一次出現的索引
//傳的實參數值的是字符串
字符串名稱.indexof(String str);
//返回指定子字符串在此字符串中第一次出現的索引
字符串名稱.indexof(int ch,fromIndex);
//從指定的索引起(含起步的位置),返回指定子字符串在此字符串中第一次出現的索引(首字母)
若爲找到就都是返回-1
//示例demo
public class String01 {
    public static void main(String[] args) {
        String name="The tree is beautiful,The tree is green!!!";
        System.out.println("字符t出現的首次位置爲:"+name.indexOf('t'));
        System.out.println("從第6個"+"字符起,t出現的首次位置爲:"+name.indexOf('t',5));
        System.out.println("從第5個"+"字符起,t出現的首次位置爲:"+name.indexOf('t',4));
        //查找子字符串的位置
        System.out.println("子字符串出現的首次位置的第一個元素的地址爲:"+name.indexOf("tree"));
        System.out.println("從第6個"+"字符起,t出現的首次位置的第一個元素的地址爲:"+name.indexOf("tree",5));
        System.out.println("從第5個"+"字符起,t出現的首次位置的第一個元素的地址爲:"+name.indexOf("tree",4));
    }
}
//頁面xia

2.獲取對應下標的字符

 String name = "The tree is beautiful,The tree is green!!!";
//字符串.charAt(int index);
System.out.println(name.charAt(1));
//----------獲取的爲h---------

3.獲取字符串長度

 String name = "The tree is beautiful,The tree is green!!!";
 System.out.println(name.length());

4.截取字符串(2個)

字符串.subString(int beginIndex);
//獲取從beginIndex開始到結尾的子字符串
字符串.subString(int beginIndex,int endIndex);
//獲取從beginIndex(含)開始到endIndex(不含結尾)的子字符串
String name = "The tree is beautiful,The tree is green!!!";
String name = "The tree is beautiful,The tree is green!!!";       System.out.println(name.substring(4));      System.out.println(name.substring(4,7));    System.out.println(name.substring(4,8));

5.分隔字符串

//字符串.split(String regex);
//regex代表按照什麼標準劃分,會按照標準劃分成多個字符串
String name = "The tree is beautiful The tree is green!!!";
String[] str=name.split(" ");
for (int i = 0; i < str.length; i++) {
        System.out.println(str[i]);
     }
//得到的是每個單詞      

6.替換字符串

//字符串.replace(老字符串,新字符串);
//得到一個新的字符串,使得原來字符串中的老字符串變成了新字符串
String name = "The tree is beautiful,The tree is green!!!";
String name01=name.replace("tree","forest");
System.out.println(name01);
System.out.println(name);

7.拼接字符串(與+""類似)

字符串.concat(String str)
//得到一個新字符串,它爲原字符串末尾加上str字符串
 String name = "The tree is beautiful,The tree is green!!!";
String name02=name.concat("Yeah");
System.out.println(name02);
System.out.println(name);

8.判斷字符是否存在

字符串.contains(指定字符串);
//判斷指定字符串是否存在
String name = "The tree is beautiful,The tree is green!!!";      System.out.println(name.contains("tree"));  System.out.println(name.contains("Tree"));

9.英文轉換成大寫的

//字符串.toUpperCase(指定字符串);
////得到一個新的字符串,使得原來字符串中的字母全都變成大寫
String name = "The tree is beautiful,The tree is green!!!";
String name03=name.toUpperCase();
System.out.println(name03);
System.out.println(name);

10.去掉前後空格

字符串.trim(指定字符串);
////得到一個新的字符串,使得原來字符串中前後空格去掉
String name = "            The tree is beautiful,The tree is green!!!            ";
String name04=name.trim();
System.out.println(name);
System.out.println(name04);
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章