Java 13_String & BiuldString

Create a String

        String randomString = "I'm just a random string";

Use quotes in a string escape it with \

        String gotToQuote ="He said, \"I'm here\"";

Other common Escape Codes
* \n : Newline
* \b : Backspace
* ' : Apostrophe
* " : Quote
* \ : Backslash

Combine Strings with a +

        System.out.println(randomString + " " + gotToQuote);

Add other data type to the string with a +

        int numTwo = 2;
        System.out.println(randomString + " " + numTwo);

Convert primitive types to a string with toString

         * String byteString = Byte.toString(bigByte);
         * String shortString = Short.toString(bigByte);
         * String intString = Integer.toString(bigInt);
         * String longString = Long.toString(bigByte);
         * String floatString = Float.toString(bigByte);
         * String doubleString = Double.toString(bigByte);
         * String booleanString = Boolean.toString(bigByte);

Convert from String to primitives with parse

         * int stringToInt = Integer.parseInt(intString);
         * parseSort, parseLong, parseByte, parseDouble, 
         * parseBoolean, parseFloat
         */

Compare strings with equals or equalsIgnoreCase

            String uppercaseStr = "BIG";
        String lowercaseStr = "big";    
        if(uppercaseStr.equals(lowercaseStr))
        {
            System.out.println("They're equal");
        }
        
        if(uppercaseStr.equalsIgnoreCase(lowercaseStr))
        {
            System.out.println("Same letters");
        }
        
        

CharAt returns the character in a string

        System.out.println("2nd Character: " + letters.charAt(1));
        

CompareTo

                String letters = "abcde";
        String moreLetters = "fghijk";
                //compareto returns 0 if strings are equal
        // Returns a negative number if letters comes before moreLetters
        // Returns a positive number if letters comes after moreLetters
        // There is also a compareToIgnoreCase()
        System.out.println(letters.compareTo(moreLetters));

Contains()

          //Contains()returns a boolean depending on whether the 
        // String contains the String you pass it
        System.out.println(letters.contains("abc"));
        
        // endsWith() checks if the String ends with the String you pass
        System.out.println(letters.endsWith("de"));
        
        // startsWith() works similar to endsWith()

IndexOf()

         //returns the 1st index that matches the String passed
        System.out.println(letters.indexOf("cd"));
        
        // You can also specify the index to start searching from
        // indexOf(StringToLookFor, IndexStartPosition)
        
        // lastIndexOf() works like indexOf except it starts from the 
        // end of the String you are searching

length()

         //returns the number of characters in a String
        System.out.println("Length of string: " + letters.length());

Replace()

              //replaces every occurrence of the first String with
        // the second String you provide
        System.out.println(letters.replace("abc", "xy"));

split()

        // You can create an array of Strings with split()
        // You define how to break up the String using a delimiter
        // If you had 123,456 and used the delimiter "," you would
        // create the array [123,456]
        String[] letterArray = letters.split("");

toString() converts the array into a String to print it

        System.out.println(Arrays.toString(letterArray));

toCharArray()

        //toCharArray() inserts every character in the string into
        // separate indexes in an array 
        char[] charArray = letters.toCharArray();
        
        System.out.println(Arrays.toString(charArray));

substring()

        // substring() returns a String starting at the first index
        // through the last index provided
        System.out.println(letters.substring(1,4));

toUpperCase() & toLowerCase()

        // toUpperCase() converts all letters into uppercase
        // toLowerCase() does the opposite
        System.out.println(letters.toUpperCase());

trim()

        // trim() gets rid of leading and trailing white space
        System.out.println(randString.trim());

StringBiuld

A String is immutable, which means every time you change a String a new version is created in memory.If you manipulate Strings allot use a StringBuilder

Create a StringBuilder
        // It has a fixed space in memory
        StringBuilder randSB = new StringBuilder("A random string");
adds
        // append() adds anything to the end of a SB
        System.out.println(randSB.append(" again"));

    // append() permanently effected the StringBuilder
        System.out.println(randSB);
delete()
        // delete() removes part of the SB from first index to the last
        System.out.println(randSB.delete(15, 21));
        
        // deleteCharAt(index) is used to delete individual chars

capacity()

    // capacity() returns the number of indexs for the SB
        System.out.println(randSB.capacity());

ensureCapacity()

                //ensureCapacity() increases the capacity for the SB
        randSB.ensureCapacity(60);
        System.out.println(randSB.capacity());

length()

        // length() returns the number of characters in the SB
        System.out.println(randSB.length());

trimToSize()

        // trimToSize() forces capacity to equal length
        randSB.trimToSize();

insert()

    // insert() inserts at the index you provide anything
        System.out.println(randSB.insert(1, "nother"));

toString

    // toString converts a SB into a String
        String oldSB = randSB.toString();
    
        /* StringBuilders also have the same methods as Strings
         * charAt(), indexOf(), lastIndexOf(), subString()
         */
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末动分,一起剝皮案震驚了整個(gè)濱河市乏沸,隨后出現(xiàn)的幾起案子交播,更是在濱河造成了極大的恐慌拜马,老刑警劉巖躁劣,帶你破解...
    沈念sama閱讀 211,884評(píng)論 6 492
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異乳怎,居然都是意外死亡谜酒,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,347評(píng)論 3 385
  • 文/潘曉璐 我一進(jìn)店門芦鳍,熙熙樓的掌柜王于貴愁眉苦臉地迎上來嚷往,“玉大人,你說我怎么就攤上這事柠衅∑と剩” “怎么了?”我有些...
    開封第一講書人閱讀 157,435評(píng)論 0 348
  • 文/不壞的土叔 我叫張陵菲宴,是天一觀的道長(zhǎng)贷祈。 經(jīng)常有香客問我,道長(zhǎng)喝峦,這世上最難降的妖魔是什么势誊? 我笑而不...
    開封第一講書人閱讀 56,509評(píng)論 1 284
  • 正文 為了忘掉前任,我火速辦了婚禮谣蠢,結(jié)果婚禮上粟耻,老公的妹妹穿的比我還像新娘查近。我一直安慰自己,他們只是感情好挤忙,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,611評(píng)論 6 386
  • 文/花漫 我一把揭開白布霜威。 她就那樣靜靜地躺著,像睡著了一般册烈。 火紅的嫁衣襯著肌膚如雪戈泼。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,837評(píng)論 1 290
  • 那天赏僧,我揣著相機(jī)與錄音大猛,去河邊找鬼。 笑死淀零,一個(gè)胖子當(dāng)著我的面吹牛挽绩,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播窑滞,決...
    沈念sama閱讀 38,987評(píng)論 3 408
  • 文/蒼蘭香墨 我猛地睜開眼琼牧,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼恢筝!你這毒婦竟也來了哀卫?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 37,730評(píng)論 0 267
  • 序言:老撾萬榮一對(duì)情侶失蹤撬槽,失蹤者是張志新(化名)和其女友劉穎此改,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體侄柔,經(jīng)...
    沈念sama閱讀 44,194評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡共啃,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,525評(píng)論 2 327
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了暂题。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片移剪。...
    茶點(diǎn)故事閱讀 38,664評(píng)論 1 340
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖薪者,靈堂內(nèi)的尸體忽然破棺而出纵苛,到底是詐尸還是另有隱情,我是刑警寧澤言津,帶...
    沈念sama閱讀 34,334評(píng)論 4 330
  • 正文 年R本政府宣布攻人,位于F島的核電站,受9級(jí)特大地震影響悬槽,放射性物質(zhì)發(fā)生泄漏怀吻。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,944評(píng)論 3 313
  • 文/蒙蒙 一初婆、第九天 我趴在偏房一處隱蔽的房頂上張望蓬坡。 院中可真熱鬧猿棉,春花似錦、人聲如沸渣窜。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,764評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)乔宿。三九已至位迂,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間详瑞,已是汗流浹背掂林。 一陣腳步聲響...
    開封第一講書人閱讀 31,997評(píng)論 1 266
  • 我被黑心中介騙來泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留坝橡,地道東北人泻帮。 一個(gè)月前我還...
    沈念sama閱讀 46,389評(píng)論 2 360
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像计寇,于是被迫代替她去往敵國(guó)和親锣杂。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,554評(píng)論 2 349

推薦閱讀更多精彩內(nèi)容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,309評(píng)論 0 10
  • pyspark.sql模塊 模塊上下文 Spark SQL和DataFrames的重要類: pyspark.sql...
    mpro閱讀 9,448評(píng)論 0 13
  • 腰俞穴 督脈第二個(gè)穴道叫腰俞番宁,在尾椎骨上元莫,脊椎第二十一椎,腰俞可治月經(jīng)不順蝶押。 命門與陽(yáng)關(guān)穴 命門是第十四椎踱蠢,在肚臍...
    了凡工作室閱讀 1,522評(píng)論 0 2
  • “臭丫頭,今天是我的生日你也敢忘嗎!” “不敢不敢棋电,這么多年了哪里會(huì)忘啊茎截,這不最近事太多了嘛,而且這不給你打電話了...
    西艽閱讀 237評(píng)論 0 0
  • #高效閱讀實(shí)踐營(yíng)# Day 5/ 21 學(xué)號(hào)111+慕斯精靈+山東 [書籍名稱]《餐飲O2O你也學(xué)得會(huì)》 [閱...
    親愛的艾糖閱讀 137評(píng)論 0 0