String類

1.實(shí)例化String對(duì)象
直接為String類賦值,字符串是String類匿名對(duì)象,若一個(gè)字符串已經(jīng)被一個(gè)名稱所引用,以后再有相同的聲明時(shí),不會(huì)再重新開(kāi)辟空間
,即共享設(shè)計(jì),內(nèi)容重復(fù)時(shí),將對(duì)象指向已存在的實(shí)例空間

public class TestJava {
    public static void main(String[] args) {    
          String name = "LiXingHua";
          System.out.print("姓名:"+“hello".equals("hello"));
    }
}
public class TestJava {
    public static void main(String[] args) {   
        String str1 = "hello";
        String str2 = new String("hello");
        String str3 = "hello";
        System.out.println(str1 == str2);
        System.out.println(str1 == str3);
    }
}

直接調(diào)用構(gòu)造方法,使用new時(shí),無(wú)論如何都會(huì)再開(kāi)辟空間

public class TestJava {
    public static void main(String[] args) {    
          String name = new String("lixinghua");
          System.out.print("姓名:"+name);
    }
}

2.判斷字符串是否相等,不可用==,其比較的時(shí)是地址
可用equals(str)

public class TestJava {
    public static void main(String[] args) {   
        String str1 = "hello";
        String str2 = new String("hello");
        System.out.print(str1.equals(str2));
    }
}

3.字符串一旦聲明,不可改變,

public class TestJava {
    public static void main(String[] args) {   
        String str1 = new String("hello");
        String str2 = "hello";
        str1 = str1 + "world";
        str2 = str2 + "world";
        System.out.println(str1);
        System.out.println(str2);
    }
}

上面實(shí)際是將str斷開(kāi)在連接,原字符串并未改變。
但可用StringBuffer類完成
4.字符串與字符數(shù)組的轉(zhuǎn)換

public class TestJava {
    public static void main(String[] args) {   
        String str1 = new String("hello");
        char c[] = str1.toCharArray();
        for(int i = 0; i < c.length; i++) {
            System.out.print(c[i]+"\t");
        }
        System.out.println("");
        String str2 = new String(c);
        String str3 = new String(c,0,3);
        System.out.println(str1);
        System.out.println(str2);
        System.out.println(str3);
    }
}

5.從字符串取出特定字符

public class TestJava {
    public static void main(String[] args) {   
        String str1 = new String("hello");
        System.out.println(str1.charAt(0));
    }
}

6.與byte數(shù)組的轉(zhuǎn)換,與char數(shù)組風(fēng)格類似

public class TestJava {
    public static void main(String[] args) {   
        String str1 = new String("hello");
        byte b[] = str1.getBytes();
        System.out.println(new String(b));
        System.out.println(new String(b,1,3));
    }
}

7.取得字符串長(zhǎng)度

str1.length();

8.查找指定字符是否存在

public class StringAPIDemo05{
    public static void main(String args[]){
        String str1 = "abcdefgcgh" ;                // 聲明字符串
        System.out.println(str1.indexOf("c")) ;     // 查到返回位置
        System.out.println(str1.indexOf("c",3)) ;   // 查到返回位置,從第4個(gè)位置開(kāi)始查找
        System.out.println(str1.indexOf("x")) ;     // 沒(méi)有查到返回-1
    }
};

9.去掉左右空格

public class StringAPIDemo06{
    public static void main(String args[]){
        String str1 = "    hello    " ;     // 定義字符串
        System.out.println(str1.trim()) ;   // 去掉左右空格后輸出
    }
};

10.字符串截取

public class StringAPIDemo07{
    public static void main(String args[]){
        String str1 = "hello world" ;       // 定義字符串
        System.out.println(str1.substring(6)) ; // 從第7個(gè)位置開(kāi)始截取
        System.out.println(str1.substring(0,5)) ; // 截取0~5個(gè)位置的內(nèi)容
    }
};

11.按指定字符串拆分字符串

public class StringAPIDemo08{
    public static void main(String args[]){
        String str1 = "hello world" ;       // 定義字符串
        String s[] = str1.split(" ") ;      // 按空格進(jìn)行字符串的拆分
        for(int i=0;i<s.length;i++){        // 循環(huán)輸出
            System.out.println(s[i]) ;
        }
    }
};

12.字符串大小寫轉(zhuǎn)換

public class StringAPIDemo09{
    public static void main(String args[]){
        System.out.println("將\"hello world\"轉(zhuǎn)成大寫:" + "hello world".toUpperCase()) ;
        System.out.println("將\"HELLO WORLD\"轉(zhuǎn)成小寫:" + "HELLO WORLD".toLowerCase()) ;
    }
};

13.判斷是否以指定字符串開(kāi)頭或結(jié)尾

public class StringAPIDemo10{
    public static void main(String args[]){
        String str1 = "**HELLO" ;           // 定義字符串
        String str2 = "HELLO**" ;           // 定義字符串
        if(str1.startsWith("**")){          // 判斷是否以“**”開(kāi)頭
            System.out.println("(**HELLO)以**開(kāi)頭") ;
        }
        if(str2.endsWith("**")){            // 判斷是否以“**”結(jié)尾
            System.out.println("(HELLO**)以**結(jié)尾") ;
        }
    }
};

14.不區(qū)分大小寫進(jìn)行字符串比較

public class StringAPIDemo11{
    public static void main(String args[]){
        String str1 = "HELLO" ;         // 定義字符串
        String str2 = "hello" ;         // 定義字符串
        System.out.println("\"HELLO\" equals \"hello\" " + str1.equals(str2)) ;
        System.out.println("\"HELLO\" equalsIgnoreCase \"hello\" "
                + str1.equalsIgnoreCase(str2)) ;    // 不區(qū)分大小寫的比較
    }
};

15.將指定字符串替換成其他字符串

public class StringAPIDemo12{
    public static void main(String args[]){
        String str = "hello" ;          // 定義字符串
        String newStr = str.replaceAll("l","x") ;   // 現(xiàn)在將所有的l替換成x
        System.out.println("替換之后的結(jié)果:" + newStr) ;
    }
};
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 【轉(zhuǎn)載】原文地址:std::string詳解作者:kieven2008 之所以拋棄char*的字符串而選用C++標(biāo)...
    VAYY閱讀 706評(píng)論 0 2
  • 一、String 類 1、定義: 1、從概念上講,java字符串就是Unicode字符序列。每個(gè)用雙引號(hào)括起來(lái)的字...
    玉圣閱讀 1,744評(píng)論 0 1
  • 在編寫程序的過(guò)程中,不了避免的要用到字符串,所以String類的常用方法的用法是必須掌握的。學(xué)習(xí)一個(gè)類的使用方法最...
    Geg_Wuz閱讀 1,497評(píng)論 0 4
  • 1.String 類的Api文檔如下 public final class String extends Obje...
    JC_Hou閱讀 1,948評(píng)論 0 6
  • 突然想起幾年前看快樂(lè)大本營(yíng),那期的嘉賓是韓庚。主持人問(wèn)他挑女友最看重哪一點(diǎn)?韓庚說(shuō)“腿”,說(shuō)話的時(shí)候看了一眼謝...
    荒蠻故事閱讀 688評(píng)論 2 5

友情鏈接更多精彩內(nèi)容