String類

字符串概述

  • 字符串:就是由多個字符組成的一串數據勇哗,也可以看成是一個字符數組
  • 通過查看API,我們也可以知道
    A:字符串字面值“abc”也可以看成是一個字符串數組
    B:字符串是常量,一旦被賦值,就不可以被更改
構造方法
  • public String();
    (空構造)
  • public String(byte[] bytes);
    (把字節(jié)數組轉成字符串)
  • public String(byte[] bytes,int index(索引),int length);
    (把字節(jié)數組的一部分轉成字符串)
  • public String(char[] value);
    (把字符數組轉成字符串)
  • public String(char[] value,int index(索引),int count);
    (把字符數組的一部分轉成字符串)
  • 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é)數組轉成字符串)
    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é)數組的一部分轉成字符串)
    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);
    //(把字符數組轉成字符串)
    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);
    //(把字符數組的一部分轉成字符串)
    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)根灯;
    (返回指定字符在此字符串中第一次出現處的索引)
    這里為什么是int類型而不是char類型呢?
    原因是‘a’和97其實都可以代表a
  • int indexOf(String str);
    (返回指定字符串在此字符串中第一次出現處的索引)
  • int indexOf(int ch,int fromIndex);
    (返回指定字符在此字符串中從指定位置后第一次出現處的索引)
  • int indexOf(String str,int fromIndex);
    (返回指定字符串在此字符串中從指定位置后第一次出現處的索引)
  • 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);
        //(返回指定字符在此字符串中第一次出現處的索引)
        System.out.println("s.indexOf:"+s.indexOf("l"));
        System.out.println("------------------------");
        //int indexOf(String str);
        //(返回指定字符串在此字符串中第一次出現處的索引)
        System.out.println("s.indexOf:"+s.indexOf("low"));
        System.out.println("------------------------");
        //這個位置說的是字符串首字母出現的位置
        //int indexOf(int ch,int fromIndex);
        //(返回指定字符在此字符串中從指定位置后第一次出現處的索引)
        System.out.println("s.indexOf:"+s.indexOf("l",4));
        System.out.println("------------------------");
        //int indexOf(String str,int fromIndex);
        //(返回指定字符串在此字符串中從指定位置后第一次出現處的索引)
        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));
    }*/
    //如果長度特別長强饮,不可能去數,所以我們就用到了長度方法
    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é)數組)
  • char[] tocharArray();
    (把字符串轉換為字符數組)
  • static String valueOf(char[] chs);
    (把字符數組轉換成字符串)
  • static String valueOf(int i);
    (把int類型的數據轉換成字符串)
    注:String類型的valueOf()方法可以把任何類型的數據轉換成字符串
  • String toLowerCase()由桌;
    (把字符串轉成小寫)
  • String toUpperCase();
    (把字符串轉成大寫)
  • String concat(String str);
    (把字符串拼接)
public class Stringdemo6 {
    public static void main(String[] args) {
        //定義一個字符串對象
        String s = "JavaSE";
        //byte[] getbytes();
        //(把字符串轉換為字節(jié)數組)
        byte[] bys = s.getBytes();
        for(int x = 0;x<bys.length;x++) {
            System.out.println(bys[x]);
        }
        System.out.println("--------------------");
        //char[] tocharArray();
        //(把字符串轉換為字符數組)
        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);
        //(把字符數組轉換成字符串)
        String ss  =  String.valueOf(chs);
        System.out.println(ss);
        System.out.println("--------------------");
        //static String valueOf(int i);
        //(把int類型的數據轉換成字符串)
        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
//哈希碼值相減
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市剪廉,隨后出現的幾起案子娃循,更是在濱河造成了極大的恐慌,老刑警劉巖斗蒋,帶你破解...
    沈念sama閱讀 223,126評論 6 520
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件捌斧,死亡現場離奇詭異,居然都是意外死亡泉沾,警方通過查閱死者的電腦和手機捞蚂,發(fā)現死者居然都...
    沈念sama閱讀 95,421評論 3 400
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來跷究,“玉大人洞难,你說我怎么就攤上這事〗页” “怎么了?”我有些...
    開封第一講書人閱讀 169,941評論 0 366
  • 文/不壞的土叔 我叫張陵色冀,是天一觀的道長潭袱。 經常有香客問我,道長锋恬,這世上最難降的妖魔是什么屯换? 我笑而不...
    開封第一講書人閱讀 60,294評論 1 300
  • 正文 為了忘掉前任,我火速辦了婚禮与学,結果婚禮上彤悔,老公的妹妹穿的比我還像新娘。我一直安慰自己索守,他們只是感情好晕窑,可當我...
    茶點故事閱讀 69,295評論 6 398
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著卵佛,像睡著了一般杨赤。 火紅的嫁衣襯著肌膚如雪敞斋。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,874評論 1 314
  • 那天疾牲,我揣著相機與錄音植捎,去河邊找鬼。 笑死阳柔,一個胖子當著我的面吹牛焰枢,可吹牛的內容都是我干的。 我是一名探鬼主播舌剂,決...
    沈念sama閱讀 41,285評論 3 424
  • 文/蒼蘭香墨 我猛地睜開眼济锄,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了架诞?” 一聲冷哼從身側響起拟淮,我...
    開封第一講書人閱讀 40,249評論 0 277
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎谴忧,沒想到半個月后很泊,有當地人在樹林里發(fā)現了一具尸體,經...
    沈念sama閱讀 46,760評論 1 321
  • 正文 獨居荒郊野嶺守林人離奇死亡沾谓,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 38,840評論 3 343
  • 正文 我和宋清朗相戀三年委造,在試婚紗的時候發(fā)現自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片均驶。...
    茶點故事閱讀 40,973評論 1 354
  • 序言:一個原本活蹦亂跳的男人離奇死亡昏兆,死狀恐怖,靈堂內的尸體忽然破棺而出妇穴,到底是詐尸還是另有隱情爬虱,我是刑警寧澤,帶...
    沈念sama閱讀 36,631評論 5 351
  • 正文 年R本政府宣布腾它,位于F島的核電站跑筝,受9級特大地震影響,放射性物質發(fā)生泄漏瞒滴。R本人自食惡果不足惜曲梗,卻給世界環(huán)境...
    茶點故事閱讀 42,315評論 3 336
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望妓忍。 院中可真熱鬧虏两,春花似錦、人聲如沸世剖。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,797評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽旁瘫。三九已至引颈,卻和暖如春耕皮,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背蝙场。 一陣腳步聲響...
    開封第一講書人閱讀 33,926評論 1 275
  • 我被黑心中介騙來泰國打工凌停, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人售滤。 一個月前我還...
    沈念sama閱讀 49,431評論 3 379
  • 正文 我出身青樓罚拟,卻偏偏與公主長得像,于是被迫代替她去往敵國和親完箩。 傳聞我的和親對象是個殘疾皇子赐俗,可洞房花燭夜當晚...
    茶點故事閱讀 45,982評論 2 361

推薦閱讀更多精彩內容