Java第十二天

60.字符串面試

(1)概述    String  str = 「abc」;字符串是類,是引用的數據類型    數組

(2)字符串的常量spa

String  str = 「abc」;3d

這是定義了一個字符串的常量。常量是不可改變的量,字符串是不能改變的。指針

public class StringTest {
    public static void main(String[] args) {
        String str="moni";
        //這裏不是作加等,在字符串上作拼接實際是新產生字符串
        str+="ca";
        System.out.println(str);
    }
}

特色:對象

字符串常量是共享的(在數據共享區中有一個字符串常量池)blog

字符串不能變化,若是咱們在字符串上作拼接,實際上是新產生一個字符串。索引

(3)字符串的構造器ci

字符串常量是一種定義的方式字符串

字符串須要有其餘形式的構造器,好比能夠經過字符的數組,字節數組,io流中的數據等等 。

public class StringTest1 {
    public static void main(String[] args) {
// 第一種方式
        String s=new String();
        System.out.println(s);

        String s1="";
//第二種方式經過字節數組來建立字符串
        byte[] b=new byte[]{88,89,90,100,98,99};
        String bs=new String(b);
        System.out.println(bs);
//第三種方式經過字符節數組來建立字符串,offset是索引的位置,length表示引用的長度
        String bs2=new String(b,1,4);
        System.out.println(bs2);
//第四種經過字節數組來建立字符串
        char[] c={'m','o','n','i','c','a'};
        String cs=new String(c);
        System.out.println(cs);
//第五種經過字節數組來建立字符串offset是索引的位置,count表示引用的長度
        String cs2=new String(c,2,4);
        System.out.println(cs2);
//第六種經過字符串常量建立字符串
        String ss=new String("original");
        System.out.println(ss);
    }
}

(4)面試題

String s1 = new String (「abc」);

String s2 = 「abc」;

有什麼區別?

new String (「abc」);若是沒有在字符串的常量池中沒有abc就會在堆中建立一個對象,同時又在字符串常量池中也建立一個對象,而後用堆中的對象來引用字符串常量池中對象。實則建立了2個對象。

new String (「abc」); 若是在字符串的常量池中有abc,那麼就會在堆中建立一個對象,直接引用已經存在的字符串常量池中的那個對象。

String s2 = 「abc」;是直接在字符串的常量池種建立一個abc

(5)空字符串和null的區別

空字符串是有效的引用,有地址的,只不過是內容的長度是0

null這個引用是空引用,不能使用。使用必定會報空指針的錯誤。引用數據類型的默認值是null。

(6)字符串的判斷方法

public static void main(String[] args) {
        String s="i love you!";
//    boolean endsWith(String suffix)判斷一個字符串是否以某一個字符串爲後綴
        boolean b1=s.endsWith("!");
        System.out.println(b1);

        String s1="I Love You!";
//    boolean equals(Object anObject) 判斷兩個字符串的值是否相等
        boolean b2=s.equals(s1);
        System.out.println(b2);
//    boolean equalsIgnoreCase(String anotherString) 判斷兩個字符串忽略大小寫後是否相等
        boolean b3=s.equalsIgnoreCase(s1);
        System.out.println(b3);
//    boolean contains(CharSequence s) 判斷一個字符串是否包含一個子字符串
        boolean b4=s.contains("love");
        System.out.println(b4);
//    boolean startsWith(String prefix) 判斷一個字符串是否以某一個字符串爲開頭
        boolean b5=s.startsWith("i");
        System.out.println(b5);
//    boolean isEmpty() 判斷一個字符串是不是空串
        boolean b6="".isEmpty();
        System.out.println(b6);
//判斷一個字符串是不是空串, 最好把空串的常量放在前面,防止空指針異常
        boolean b7="".equals(s);
        System.out.println(b7);

    }

(7)字符串的獲取功能

public static void main(String[] args) {
        String s="i love you!";
//    int length() 得到字符串的長度,區別於數組的長度是屬性
        int length=s.length();
        System.out.println(length);
//     char charAt(int index)  根據索引號得到字符
        char ca=s.charAt(5);
        System.out.println(ca);
//     int indexOf(int ch)  根據指定字符(ascii碼對應字符)得到該字符在字符串中首次出現的位置
        int idi=s.indexOf(101);
        System.out.println(idi);
//    int indexOf(String str)  根據指定字符串得到在母字符串中第一次出現的索引
        int ids=s.indexOf("ve");
        System.out.println(ids);
    }

/*練習:計算一個字符串中大寫字母和小寫字母還有數字的數量
23aosdf23aosdfSDFGsfdgloasasdfasfgsrgqADF2453sadfgag*/
public class StringTest4 {
    public static void main(String[] args) {
        int num=0;
        int upper=0;
        int downer=0;
        String s="23aosdf23aosdfSDFGsfdgloasasdfasfgsrgqADF2453sadfgag";
        for (int i = 0; i < s.length(); i++) {
            int asc=s.charAt(i);
            if(asc>47&&asc<58){
                num++;
            }
            if(asc>64&&asc<91){
                upper++;
            }
            if(asc>96&&asc<123){
                downer++;
            }
        }
        System.out.println("字符串中大寫字母數量是:"+upper);
        System.out.println("字符串中小寫字母數量是:"+downer);
        System.out.println("字符串中數字數量是:"+num);
    }
}

相關文章
相關標籤/搜索