国产精品天干天干,亚洲毛片在线,日韩gay小鲜肉啪啪18禁,女同Gay自慰喷水

歡迎光臨散文網 會員登陸 & 注冊

java字符串String類用法詳解

2022-07-12 17:58 作者:虛云幻仙  | 我要投稿

/**
* String類用法詳解
*/

public class TestString {
? ?public static void main(String[] args) {
? ? ? ?String s1 = "abcde";
? ? ? ?String s2 = s1.substring(1,4);
? ? ? ?//字符串s1的value為{a,b,c,d,e} ?.substring()方法用value[1到3] new一個String返回 (1,4)包頭1不包尾4
? ? ? ?System.out.println(s2+"的結果為bcd");
? ? ? ?System.out.println((s2==s1)+"的結果為false");
? ? ? ?//s1沒有改變還是abcde s1和s2是兩個對象
? ? ? ?System.out.println("abcde"=="abcde");
? ? ? ?//常量隨類進入方法區(qū)的常量池中 常量唯一 直接寫"abcde"時永遠指向同一個常量
? ? ? ?String s3 = "abc"+"de";
? ? ? ?System.out.println((s1==s3)+"的結果為true");
? ? ? ?//編譯階段會將s3 = "abc"+"de" 變?yōu)?s3 = "abcde"
? ? ? ?String s4 = "abc";
? ? ? ?String s5 = "de";
? ? ? ?String s6 = s4+s5;
? ? ? ?System.out.println((s6==s3)+"的結果為false");
? ? ? ?//編譯器無法識別兩個變量的值 所以s4+s5會new新的String返回
? ? ? ?System.out.println(s6.equals(s3)+"的結果為true");
? ? ? ?//重寫Object.equals(Object obj) 如果地址一樣則true 地址不同再判斷s3 instanceof實例 String類 屬于則再判斷兩個字符串長度是否相同 相同則逐字比較
? ? ? ?//s6 s3指向不同對象 但字符串內容一樣 返回true

? ? ? ?String s7 = s4.concat(s5);
? ? ? ?System.out.println(s7+"結果為abcde"+(s7==s6)+"結果為false");
? ? ? ?//s4.concat(s5) Concatenates the specified string to the end of this string. 連接Concatenate指定的specified字符串到this字符串末尾
? ? ? ?//specified指()內的參數(shù)列表

? ? ? ?/*
? ? ? ?If the length of the argument string is 0, then this String object is returned.
? ? ? ?argument參數(shù)即specified string 如果傳參為空字符串則返回this
? ? ? ?Otherwise, a String object is returned that represents a character sequence that is the concatenation of the character sequence represented by this String object and the character sequence represented by the argument string.
? ? ? ?otherwise否則 represent代表 character字符 sequence序列 否則將把this和argument參數(shù) 這兩個字符串的字符序列連起來返回新的字符串對象
? ? ? ? */

? ? ? ?String s8 = s7.replace('b','d');
? ? ? ?System.out.println(s8+"的結果為adcde");
? ? ? ?String s9 = s8.replace('d','f');
? ? ? ?System.out.println(s9+"的結果為afcfe");
? ? ? ?//.replace(oldChar原本的字符,新的字符) 如果字符串中沒有找到oldChar則返回this不替換 如果存在oldChar則將全部oldChar替換

? ? ? ?String s10 = s9.toUpperCase();
? ? ? ?System.out.println(s10+"的結果為AFCFE 全部變成大寫");
? ? ? ?String s11 = s10.toLowerCase();
? ? ? ?System.out.println(s11+"的結果為afcfe 全部變?yōu)樾?#34;);
? ? ? ?System.out.println(s10.equalsIgnoreCase(s11)+"的結果為true 忽略大小寫比較內容");
? ? ? ?System.out.println(s10.compareTo(s11)+"的結果為-32");
? ? ? ?//s10按照Unicode字符集對s11進行比較返回int值 返回正數(shù)即s10在字典順序上位于s11后面 返回負數(shù)即s10在字典順序上先于s11
? ? ? ?// 返回兩字符串中第一個不同的字符的差 s10.charAt(k)-s11.charAt(k)
? ? ? ?// 如果這樣從前往后比較直到一個字符串比完了另一個字符串后面還有字符 則返回兩個字符串長度差 s10.length()-s11.length()
? ? ? ?//s10.charAt(0) - s11.charAt(0) ?即'A' - 'a' 在Unicode字符集中A位于a前面 所以結果為負數(shù) 兩個字符在字符集中的位置相差32


? ? ? ?System.out.println(s11.charAt(1)+"的結果為f 返回index索引1對應的字符");
? ? ? ?System.out.println(s11.length()+"的結果為5 返回字符串的長度");

? ? ? ?String s12 = " ? ? ?a ?bcde ?";
? ? ? ?System.out.println(s12.trim()+"的結果為a ?bcde");
? ? ? ?//trim修剪 將字符串兩端的空格和制表符去掉 中間的空格依然保留

? ? ? ?String s13 = "abdc ?";
? ? ? ?System.out.println(s13.startsWith("ab")+"的結果為true 判斷字符串是否以ab開頭");
? ? ? ?System.out.println(s13.endsWith("dc")+"的結果為false 判斷字符串是否以dc結束 dc后面還有兩個空格所以false");
? ? ? ?System.out.println(s13.indexOf(" ")+"的結果為4 從前往后找 返回第一個空格的index下標");
? ? ? ?System.out.println(s13.lastIndexOf(" ")+"的結果為5 從后往前找 返回的index依然是從前往后數(shù)的下標");

? ?}
}

java字符串String類用法詳解的評論 (共 條)

分享到微博請遵守國家法律
乐安县| 南宫市| 黔东| 平南县| 互助| 农安县| 邢台市| 隆回县| 内黄县| 瑞安市| 孟村| 乐昌市| 延吉市| 平顺县| 松原市| 巴马| 钦州市| 共和县| 博罗县| 萝北县| 北流市| 华坪县| 略阳县| 策勒县| 清水河县| 中超| 莱阳市| 黄冈市| 斗六市| 邻水| 阿拉善左旗| 洛宁县| 织金县| 西华县| 安多县| 庆安县| 普安县| 阿勒泰市| 赞皇县| 静安区| 政和县|