java之day6

來源:互聯網
上載者:User

標籤:中國   常量   內容   logs   rds   方法   arrays   scan   ***   

 String類

package day6;import java.util.Arrays;public class StringTest {    public static void main(String[] args) {        //    ==    比較的是兩個資料的大小        //基礎資料型別 (Elementary Data Type)的儲存地區在棧中,每一個資料都是相互獨立存在的        int i = 5;        int j = 5;        System.out.println(i==j);                //數組是引用資料類型,        //數組佔用兩塊空間:棧和堆        //棧中存放數組資料在堆中存在的首地址(數組名)        //堆中存放的是數組資料        int arr1[] = new int[2];        arr1[0] = 1;        arr1[1] = 2;        int arr2[] = new int[2];        arr2[0] = 1;        arr2[1] = 2;        System.out.println(arr1==arr2);        System.out.println(Arrays.equals(arr1, arr2));                        //普通的聲明字串的方法,只會在字串常量池裡開闢空間;        //並且在開闢之前,會檢查字串常量池裡是否已經存在;        //相同的資料,如果有,直接指向已經存在的資料,如果沒有,會在字串常量池中開闢一個新的空間;        String s = "ccy";        String s2 = "ccy";        System.out.println(s==s2);                //在堆裡開闢一個空間,存放內容,然後檢查字串常量池裡又沒有,沒有就在池裡開闢一個空間存放jereh        String s3 = new String("jereh");        String s4 = new String("jereh");        System.out.println(s3 == s4);                        String s6 = new String("jredu");        String s5 = "jredu";        System.out.println(s5 == s6);    }}

 

 

==================================================================================================================

package day6;public class Demo03 {    //.concat()    連結字串    public static void main(String[] args) {        String string = new String("nihao");        String name = new String("zhangsan");        String abc = string.concat(name);        System.out.println(abc);    }}

 

 

 

¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥

package day6;import org.omg.CORBA.INTERNAL;import day52.shuzu;public class Demo04 {    public static void main(String[] args) {        String a = new String("asdf.java");                //indexOf() 擷取字串中某個字元或字串出現的位置        System.out.println(a.indexOf("a"));                //lastIndesOf() 擷取字串中某個字元或字串最後一次出現的位置        System.out.println(a.lastIndexOf("a"));                //substring(index)從指定位置(包含)開始截取字串,知道最後一個        String newA = a.substring(4);        System.out.println(newA);                int index = a.indexOf(".");        String newA2 = a.substring(index+1);        System.out.println(newA2);                //從指定索引開始(包含),到指定索引結束(不包含)        String newA3 = a.substring(2,4);        System.out.println(newA3);                //trim()去前後空格        String s2 = "       傑 瑞 教 育               ";        String newS4 = s2.trim();        System.out.println(newS4);    }    }

 

 

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

package day6;public class Demo06 {    public static void main(String[] args) {        //分隔        String words = "長亭外 古道邊 芳草碧連天 晚風拂柳笛聲殘 夕陽山外山";        String[] printword = new String[100];        System.out.println("***原歌詞格式***\n" + words);        System.out.println("\n***拆分後歌詞格式***");        printword = words.split(" ");        for (int i = 0; i < printword.length; i++) {            System.out.println(printword[i]);        }    }}

 

 

 

¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥

package day6;public class Demo07 {    public static void main(String[] args) {        //StringBuffer        //append() 用來向stringBuffer追加內容的        StringBuffer sb = new StringBuffer("QWER");        int num = 110;        StringBuffer sb1 = sb.append("qwerty");        System.out.println(sb1);        StringBuffer sb2 = sb1.append("qwerty");        System.out.println(sb2);        StringBuffer sb3 = sb2.append(num);        System.out.println(sb3);                //String ---> StringBuffer        StringBuffer s = new StringBuffer("qwer");                //StringBuffer ---> String        sb.toString();    }}

 

 

$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

package day6;import java.util.Scanner;public class Demo08 {    public static void main(String[] args) {        //StringBuffer    insert(索引index,方法content)方法        Scanner scanner = new Scanner(System.in);        System.out.println("qingshuru");        String string = scanner.next();        StringBuffer buffer = new StringBuffer(string);        for (int i = buffer.length()-3; i > 0; i-=3) {            buffer.insert(i, ",");        }        System.out.println(buffer);        scanner.close();    }}

 

 

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

package day6;import java.util.Arrays;public class Test {    public static void main(String[] args) {        String string = "愛你,愛我,愛她,愛中國!愛";        string = " "+string+" ";        int length = string.split("愛").length-1;        System.out.println(length);    }}

 

 

&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

package day61;public class Test {    //全域變數有初始值    //局部變數沒有初始值        int i;    //OR下面那個int i = 0;        public void check(){    //    int i =0;        System.out.println(i);    }        public static void main(String[] args) {        Test test = new Test();        test.check();    }}

 

java之day6

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.