String類

字符串概述

  • 字符串:就是由多個字符組成的一串數(shù)據(jù),也可以看成是一個字符數(shù)組
  • 通過查看API,我們也可以知道
    A:字符串字面值“abc”也可以看成是一個字符串數(shù)組
    B:字符串是常量,一旦被賦值,就不可以被更改
構造方法
  • public String();
    (空構造)
  • public String(byte[] bytes);
    (把字節(jié)數(shù)組轉成字符串)
  • public String(byte[] bytes,int index(索引),int length);
    (把字節(jié)數(shù)組的一部分轉成字符串)
  • public String(char[] value);
    (把字符數(shù)組轉成字符串)
  • public String(char[] value,int index(索引),int count);
    (把字符數(shù)組的一部分轉成字符串)
  • public String(String original);
    (把字符串常量轉成字符串)
字符串方法
  • public int length();返回此字符串的長度
public class Stringdemo {
public static void main(String[] args) {
    //public String();
    //(空構造)
    String s1 = new String();
    System.out.println("s1:"+s1);
    System.out.println("s1.length:"+s1.length());
    System.out.println("------------------------");
    //public String(byte[] bytes);
    //(把字節(jié)數(shù)組轉成字符串)
    byte[] bys = {97,98,99,100,101};
    //會把字節(jié)轉換成對應的哈希碼值,從而轉換成字符
    String s2 = new String(bys);
    System.out.println("s2:"+s2);
    System.out.println("s2.length:"+s2.length());
    System.out.println("------------------------");
    //public String(byte[] bytes,int index(索引),int length);
    //(把字節(jié)數(shù)組的一部分轉成字符串)
    String s3 = new String(bys,1,3);
    //表示的是從第一個字節(jié)的位置開始,到第三個字節(jié)結束,轉換成對應的字符串
    System.out.println("s3:"+s3);
    System.out.println("s3.length:"+s3.length());
    System.out.println("------------------------");
    //public String(char[] value);
    //(把字符數(shù)組轉成字符串)
    char[] chs = {'a','b','c','d','e','張','國','榮'};
    String s4 = new String(chs);
    System.out.println("s4:"+s4);
    System.out.println("s4.length:"+s4.length());
    System.out.println("------------------------");
    //public String(char[] value,int index(索引),int count);
    //(把字符數(shù)組的一部分轉成字符串)
    String s5= new String(chs,2,5);
    System.out.println("s5:"+s5);
    System.out.println("s5.length:"+s5.length());
    System.out.println("------------------------");
    //public String(String original);
    //(把字符串常量轉成字符串)
    String s6 = "abcde";
    System.out.println("s6:"+s6);
    System.out.println("s6.length:"+s6.length());
    
}
}

結果為:
s1:
s1.length:0
------------------------
s2:abcde
s2.length:5
------------------------
s3:bcd
s3.length:3
------------------------
s4:abcde張國榮
s4.length:8
------------------------
s5:cde張國
s5.length:5
------------------------
s6:abcde
s6.length:5

String類的特點

  • 一旦被賦值就不能改變
String s = "hello";
s+="world";
system.out.println("s:"+s);

結果為:
helloworld
字符串特點:

A:字符串直接賦值的方法是先到字符串常量池里面去找,如果有就返回,如果沒有就創(chuàng)建并返回
B:一旦被賦值就不能被改變(說的是值不能變,不是字符串不能變)

CC2@0IGE~@{(L5WPR$IELIK.png

String字面值對象和構造方法創(chuàng)建對象的區(qū)別

  • String s = new String("hello");String s = "hello";
    這倆個有區(qū)別么?
    有,前者會創(chuàng)建倆個對象,后者只創(chuàng)建一個對象
  • ==:比較引用類型,比較的是地址值是否相同
  • equals:比較引用類型,默認也是比較地址值是否相同,而String()重寫了equals()方法,比較的是內容是否相同
public class Stringdemo2 {
public static void main(String[] args) {
    String s1 = new String("hello");
    String s2 = "hello";
    System.out.println(s1==s2);
    System.out.println(s1.equals(s2));
}
}


結果為:
false
true

![J14EF]8P)(FO@KS6%C4NRFX.png](https://upload-images.jianshu.io/upload_images/14016997-96df199e5a5f3eed.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

String類的判斷功能

  • boolean equals(Object obj);
    (比較字符串內容是否相同)
  • boolean equalsIgnoreCase(String str);
    (比較字符串的內容是否相同,忽略大小寫)
  • boolean contains(String str);
    (判斷大字符串中是否包含小字符串)
  • boolean startsWith(String str);
    (判斷字符串是否以某個指定的字符串開頭)
  • boolean endWith(String str);
    (判斷字符串是否以某個指定的字符串結尾)
  • boolean isEmpty();
    (判斷字符串是否為空)
    注:字符串內容為空和字符串對象為空不一樣
    例:String s = " ";//表示有內容,但是內容是空的,可以調方法
    String s = null;//表示沒有內容,不能調方法
public class Stringdemo3 {
    public static void main(String[] args) {
        //創(chuàng)建字符串對象
        String s1 = "helloworld";
        String s2 = "helloworld";
        String s3 = "Helloworld";
        //boolean equals(Object obj);
        //(比較字符串內容是否相同,區(qū)分大小寫)
        System.out.println("equals:"+s1.equals(s2));
        System.out.println("equals:"+s1.equals(s3));
        System.out.println("--------------------------");
        //boolean equalsIgnoreCase(String str);
        //(比較字符串的內容是否相同,忽略大小寫)
        System.out.println("equalsIgnoreCase:"+s1.equalsIgnoreCase(s2));
        System.out.println("equalsIgnoreCase:"+s1.equalsIgnoreCase(s3));
        System.out.println("--------------------------");
        //boolean contains(String str);
        //(判斷大字符串中是否包含小字符串)
        System.out.println("contains:"+s1.contains("hello"));
        System.out.println("contains:"+s1.contains("hw"));
        System.out.println("--------------------------");
        //boolean startsWith(String str);
        //(判斷字符串是否以某個指定的字符串開頭)
        System.out.println("startsWith:"+s1.startsWith("hello"));
        System.out.println("startsWith:"+s1.startsWith("hw"));
        System.out.println("--------------------------");
        //boolean endWith(String str);
        //(判斷字符串是否以某個指定的字符串結尾)
        System.out.println("endWith:"+s1.endsWith("hello"));
        System.out.println("endWith:"+s1.endsWith("world"));
        System.out.println("--------------------------");
        //boolean isEmpty();
        //(判斷字符串是否為空)
        System.out.println("endWith:"+s1.isEmpty());
        System.out.println("endWith:"+s1.isEmpty());
        String s4 = "";
        String s5 = null;
        System.out.println("endWith:"+s4.isEmpty());
        System.out.println("endWith:"+s5.isEmpty());//error,因為連對象都沒有
    }

}

結果為:
equals:true
equals:false
--------------------------
equalsIgnoreCase:true
equalsIgnoreCase:true
--------------------------
contains:true
contains:false
--------------------------
startsWith:true
startsWith:false
--------------------------
endWith:false
endWith:true
--------------------------
endWith:false
endWith:false
endWith:true
Exception in thread "main" java.lang.NullPointerException
    at Stringdemo3.main(Stringdemo3.java:40)

String類的獲取功能

  • int length();
    (獲取字符串的長度)
  • char charAt(int index);
    (獲取指定索引位置的字符)
  • int indexOf(int ch);
    (返回指定字符在此字符串中第一次出現(xiàn)處的索引)
    這里為什么是int類型而不是char類型呢?
    原因是‘a(chǎn)’和97其實都可以代表a
  • int indexOf(String str);
    (返回指定字符串在此字符串中第一次出現(xiàn)處的索引)
  • int indexOf(int ch,int fromIndex);
    (返回指定字符在此字符串中從指定位置后第一次出現(xiàn)處的索引)
  • int indexOf(String str,int fromIndex);
    (返回指定字符串在此字符串中從指定位置后第一次出現(xiàn)處的索引)
  • String substring(int start);
    (從指定位置開始截取字符串,默認到末尾,包含start這個索引)
  • String substring(int start,int end);
    (從指定位置開始到指定位置結束截取字符串,包括start索引,但是不包括ebd索引)
public class Stringdemo4 {
    public static void main(String[] args) {
        //定義一個字符串對象
        String s = "helloworld";
        //int length();
        //(獲取字符串的長度)
        System.out.println("s.length:"+s.length());
        System.out.println("------------------------");
        //char charAt(int index);
        //(獲取指定索引位置的字符)
        System.out.println("s.charAt:"+s.charAt(7));
        System.out.println("------------------------");
        //int indexOf(int ch);
        //(返回指定字符在此字符串中第一次出現(xiàn)處的索引)
        System.out.println("s.indexOf:"+s.indexOf("l"));
        System.out.println("------------------------");
        //int indexOf(String str);
        //(返回指定字符串在此字符串中第一次出現(xiàn)處的索引)
        System.out.println("s.indexOf:"+s.indexOf("low"));
        System.out.println("------------------------");
        //這個位置說的是字符串首字母出現(xiàn)的位置
        //int indexOf(int ch,int fromIndex);
        //(返回指定字符在此字符串中從指定位置后第一次出現(xiàn)處的索引)
        System.out.println("s.indexOf:"+s.indexOf("l",4));
        System.out.println("------------------------");
        //int indexOf(String str,int fromIndex);
        //(返回指定字符串在此字符串中從指定位置后第一次出現(xiàn)處的索引)
        System.out.println("s.indexOf:"+s.indexOf("llo",4));
        System.out.println("------------------------");
        //String substring(int start);
        //(從指定位置開始截取字符串,默認到末尾,包含start這個索引)
        System.out.println("s.indexOf:"+s.substring(1));
        System.out.println("------------------------");
        //String substring(int start,int end);
        //(從指定位置開始到指定位置結束截取字符串,包括start索引,但是不包括ebd索引)
        System.out.println("s.indexOf:"+s.substring(1,4));
        System.out.println("------------------------");
        
    }

}

結果為:
s.length:10
------------------------
s.charAt:r
------------------------
s.indexOf:2
------------------------
s.indexOf:3
------------------------
s.indexOf:8
------------------------
s.indexOf:-1
------------------------
s.indexOf:elloworld
------------------------
s.indexOf:ell
------------------------

字符串的遍歷

需求:

遍歷獲取字符串中的每一個字符

分析:

A:如何能拿到每一個字符呢?
char charAt(int index);
B:我怎么知道字符到底有多少個呢?
int length();

public class Stringdemo5 {
public static void main(String[] args) {
    String s = "helloworld";
    //原始版本
    /*System.out.println(s.charAt(0));
    System.out.println(s.charAt(1));
    System.out.println(s.charAt(2));
    System.out.println(s.charAt(3));
    System.out.println(s.charAt(4));
    System.out.println(s.charAt(5));
    System.out.println(s.charAt(6));
    System.out.println(s.charAt(7));
    System.out.println(s.charAt(8));
    System.out.println(s.charAt(9));*/
    //我們只需要從零取到九
    /*for(int x=0;x<=9;x++) {
        System.out.println(s.charAt(x));
    }*/
    //如果長度特別長,不可能去數(shù),所以我們就用到了長度方法
    for(int x=0;x<s.length();x++) {
        System.out.println(s.charAt(x));
    }
}
}

結果為:
h
e
l
l
o
w
o
r
l
d

String類的轉換功能

  • byte[] getbytes();
    (把字符串轉換為字節(jié)數(shù)組)
  • char[] tocharArray();
    (把字符串轉換為字符數(shù)組)
  • static String valueOf(char[] chs);
    (把字符數(shù)組轉換成字符串)
  • static String valueOf(int i);
    (把int類型的數(shù)據(jù)轉換成字符串)
    注:String類型的valueOf()方法可以把任何類型的數(shù)據(jù)轉換成字符串
  • String toLowerCase();
    (把字符串轉成小寫)
  • String toUpperCase();
    (把字符串轉成大寫)
  • String concat(String str);
    (把字符串拼接)
public class Stringdemo6 {
    public static void main(String[] args) {
        //定義一個字符串對象
        String s = "JavaSE";
        //byte[] getbytes();
        //(把字符串轉換為字節(jié)數(shù)組)
        byte[] bys = s.getBytes();
        for(int x = 0;x<bys.length;x++) {
            System.out.println(bys[x]);
        }
        System.out.println("--------------------");
        //char[] tocharArray();
        //(把字符串轉換為字符數(shù)組)
        char[] chs = s.toCharArray();
        for(int x = 0;x<chs.length;x++) {
            System.out.println(chs[x]);
        }
        System.out.println("--------------------");
        //static String valueOf(char[] chs);
        //(把字符數(shù)組轉換成字符串)
        String ss  =  String.valueOf(chs);
        System.out.println(ss);
        System.out.println("--------------------");
        //static String valueOf(int i);
        //(把int類型的數(shù)據(jù)轉換成字符串)
        int i = 100;
        String sss = String.valueOf(i);
        System.out.println(sss);
        System.out.println("--------------------");
        //String toLowerCase();
        //(把字符串轉成小寫)
        System.out.println("toLowerCase:"+s.toLowerCase());
        System.out.println("s:"+s);
        System.out.println("--------------------");
        //String toUpperCase();
        //(把字符串轉成大寫)
        System.out.println("toUpperCase:"+s.toUpperCase());
        System.out.println("s:"+s);
        System.out.println("--------------------");
        //String concat(String str);
        //(把字符串拼接)
        String s1 = "hello";
        String s2 = "world";
        String s3 = s1+s2;
        System.out.println(s3);
    }

}

結果為:
74
97
118
97
83
69
--------------------
J
a
v
a
S
E
--------------------
JavaSE
--------------------
100
--------------------
toLowerCase:javase
s:JavaSE
--------------------
toUpperCase:JAVASE
s:JavaSE
--------------------
helloworld

字符串的其他功能

  • 替換功能
    String replace(char old,char new);
    String replace(String old,String new);
  • 去除字符串倆空格
    String trim();
  • 按字典順序比較倆字符串
    int compareTo(String str)
    int compareToIgnoreCase(String str)
public class Stringdemo7 {
    public static void main(String[] args) {
        //替換功能
        String s1  =  "helloworld";
        String s2  =  s1.replace("l", "k");
        String s3  = s1.replace("owo","ak47");
        System.out.println("s1:"+s1);
        System.out.println("s1:"+s2);
        System.out.println("s1:"+s3);
        System.out.println("-----------------");
        //去除字符串倆空格
        String s4 = "helloworld";
        String s5 = s4.trim();
        System.out.println("s4:"+s4 +"-------------");
        System.out.println("s5:"+s5 +"-------------");
        System.out.println("-----------------");
        //按字典順序比較倆字符串
        String s6 = "hello";
        String s7 = "hello";
        String s8 = "abc";
        String s9 = "syz";
        System.out.println(s6.compareTo(s7));
        System.out.println(s6.compareTo(s8));
        System.out.println(s6.compareTo(s9));
        
    }

}

結果為:
s1:helloworld
s1:hekkoworkd
s1:hellak47rld
-----------------
s4:helloworld-------------
s5:helloworld-------------
-----------------
0
7
-11
//哈希碼值相減
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容