java基礎(chǔ)的詳細(xì)了解第五天
1、String類
1.1兩種對(duì)象實(shí)例化方式
對(duì)于String在之前已經(jīng)學(xué)習(xí)過(guò)了基本使用,就是表示字符串,那么當(dāng)時(shí)使用的形式采取了直接賦值:
public class StringText{ public static void main(String args[]){ String str =new String( "Hello"); //構(gòu)造方法 System.out.print(str); } }
對(duì)于String而言肯定是一個(gè)類,那么程序之中出現(xiàn)的str應(yīng)該就是這個(gè)類的對(duì)象,那么就證明以上的賦值操作實(shí)際上就表示要為String類的對(duì)象進(jìn)行實(shí)例化操作。
但String畢竟是一個(gè)類,那么類之中一定會(huì)存在構(gòu)造方法,String類的構(gòu)造:
public class StringText{ public static void main(String args[]){ String str =new String( "Hello"); //構(gòu)造方法 System.out.print(str); } }
發(fā)現(xiàn)現(xiàn)在也可以通過(guò)構(gòu)造方法為String類對(duì)象實(shí)例化。
1.2字符串比較
如果現(xiàn)在有兩個(gè)int型變量,如果想要知道是否相等,使用“==”進(jìn)行驗(yàn)證。
public class StringText{ public static void main(String args[]){ int x = 10; int y = 10; System.out.print(x==y); } }
換成String
public class StringText{ public static void main(String args[]){ String str1 = "Hello"; String str2 = new String("Hello"); String str3 = str2; //引用傳遞 System.out.print(str1== str2); //false System.out.print(str1== str3); //false System.out.print(str2== str3); //ture } }
現(xiàn)在使用了“==”的確是完成了相等的判斷,但是最終判斷的是兩個(gè)對(duì)象(現(xiàn)在的對(duì)象是字符串)判斷是否相等,屬于數(shù)值判斷------判斷的是兩個(gè)對(duì)象的內(nèi)存地址數(shù)值,并沒(méi)有判斷內(nèi)容,而想要完成字符串內(nèi)容的判斷,則就必須使用到String類的操作方法:public Boolean equals(String str)(將方法暫時(shí)變了)
public class StringText{ public static void main(String args[]){ String str1 = "Hello"; String str2 = new String("Hello"); String str3 = str2; //引用傳遞 System.out.print(str1.equals(str2)); //ture System.out.print(str2.equals(str3)); //ture System.out.print(str2.equals(str3)); //ture } }
1.3字符串常量是String的匿名對(duì)象
如果在程序之中定義了字符串(使用“””),那么這個(gè)就表示一個(gè)String對(duì)象,因?yàn)樵诟鱾€(gè)語(yǔ)言之中沒(méi)有關(guān)于字符串?dāng)?shù)據(jù)類型的定義,而Java將其簡(jiǎn)單的處理了,所以感覺(jué)上存在了字符串?dāng)?shù)據(jù)類型。
**范例:**驗(yàn)證字符串是對(duì)象的概念
public class NiMing{ public static void main(String args[]){ String str = "Hello"; System.out.print("Hello".equals(str)); //通過(guò)字符串調(diào)用方法 } }
匿名對(duì)象可以調(diào)用類之中的方法與屬性,以上的字符串可以調(diào)用了equals()方法,那么它一定是一個(gè)對(duì)象。
**小技巧:**關(guān)于字符串與字符串常量的判斷
例如:在實(shí)際工作之中會(huì)有這樣一種操作,要求用戶輸入一個(gè)內(nèi)容,之后判斷此內(nèi)容是否與指定內(nèi)容相同。
public class NiMing{ public static void main(String args[]){ String str = "Hello"; if(str.equals("Hello")){ System.out.print("條件滿足"); } } }
但,既然數(shù)據(jù)是用戶自己輸入,那么就有可能沒(méi)有輸入內(nèi)容。
public class TestDemo1{ public static void main(String args[]){ String str = null; if(str.equals("Hello")){ System.out.print("條件滿足"); } } } //報(bào)錯(cuò) Exception in thread "main" java.lang.NullPointerException at NiMing.main(TestDemo1.java:4) //現(xiàn)在將代碼反過(guò)來(lái)操作: public class TestDemo1{ public static void main(String args[]){ String str = null; if("Hello".equals(str)){ System.out.print("條件滿足"); } } }
因?yàn)樽址A渴悄涿麑?duì)象,匿名對(duì)象不可能為null。
1.4String兩種實(shí)例化方式區(qū)別
1、分析直接賦值方式
String str = "Hello"; //定義字符串
發(fā)現(xiàn)現(xiàn)在只開(kāi)辟額一塊堆內(nèi)存空間和一塊棧內(nèi)存空間。
2、構(gòu)造方法賦值
String str = new String("Hello");
使用構(gòu)造方法賦值的方式開(kāi)辟的字符串對(duì)象,實(shí)際上會(huì)開(kāi)辟兩塊空間,其中有一塊空間就愛(ài)那個(gè)成為垃圾。
public class TestDemo2{ public static void main(String args[]){ String str1 = new String("Hello"); String str2 = "Hello"; //入池 String str3 = "Hello"; //使用池中對(duì)象 System.out.print(str1==str2); //false System.out.print(str2==str3); // ture System.out.print(str1==str3); // false } }
通過(guò)上面的程序可以發(fā)現(xiàn),使用構(gòu)造方法實(shí)例化String對(duì)象,不會(huì)入池,只能自己使用??墒窃赟tring類之中為了方便操作提供了一種稱為手工入池的方法:public String intern()。
public class TestDemo2{ public static void main(String args[]){ String str1 = new String("Hello").intern(); //手工入池 String str2 = "Hello"; //入池 String str3 = "Hello"; //使用池中對(duì)象 System.out.print(str1==str2); //ture System.out.print(str2==str3); //ture System.out.print(str1==str3); //ture } }
1.5字符串常量不可改變
字符串類的操作特點(diǎn)決定:字符串不可能去修改里面的內(nèi)容。
public class TestDemo3{ public static void main(String args[]){ String str = "Hello"; str += "World"; str += "!!!"; System.out.print(str); } }
通過(guò)以上的代碼可以發(fā)現(xiàn),字符串內(nèi)容的更改,實(shí)際上改變的是字符串對(duì)象的引用過(guò)程,那么一下的代碼應(yīng)該盡量避免:
public class TestDemo3{ public static void main(String args[]){ String str = "Hello"; for(int x=0;x<1000;x++){ str += x; } System.out.print(str); } }
- 字符串賦值只用直接賦值模式進(jìn)行完成
- 字符串的比較采用equals()方法進(jìn)行實(shí)現(xiàn)字
- 符串沒(méi)有特殊的情況不要改變太多
1.6開(kāi)發(fā)中String必用
任何一個(gè)類的文檔由如下幾個(gè)部分組成
- 類的相關(guān)定義,包括這個(gè)類的名字,有哪些父類,有哪些接口。
- 類的相關(guān)簡(jiǎn)介。包括基本使用
- 成員摘要(field):屬性就是一種成員,會(huì)列出所有成員的信息項(xiàng)
- 構(gòu)造方法說(shuō)明(Constructor),列出所有構(gòu)造方法的信息
- 方法信息(Method),所有類中定義好的可以使用的方法
- 成員、構(gòu)造、方法的詳細(xì)信息
1.7字符串和字符數(shù)組
字符串就是一個(gè)字符數(shù)組,所有在String類中有字符串轉(zhuǎn)變?yōu)樽址麛?shù)組,字符數(shù)組轉(zhuǎn)換為字符串的方法。
方法名稱 | 類型 | 描述 |
---|---|---|
public String(char[] value) | 構(gòu)造 | 將字符數(shù)組中的所有內(nèi)容變?yōu)樽址?/td> |
public String(char[] value, int offset, int count) | 構(gòu)造 | 將字符數(shù)組中的所有內(nèi)容變?yōu)樽址?offset-開(kāi)始 count-個(gè)數(shù) |
public char charAt(int index) | 普通 | 返回char指定字符的索引值 |
public char[] toCharArray() | 普通 | 將字符串轉(zhuǎn)化為字符數(shù)組 |
charAt方法
public class TestDemo4{ public static void main(String args[]){ String str = "Hello"; System.out.println(str.charAt(0)); //如果現(xiàn)在超過(guò)了字符串的長(zhǎng)度,則會(huì)產(chǎn)生異常StringIndexOutOfBoundsException System.out.println(str.charAt(10)); } }
字符串和字符數(shù)組的轉(zhuǎn)化是重點(diǎn)
//字符串轉(zhuǎn)化為字符數(shù)組 public class TestDemo4{ public static void main(String args[]){ String str = "helloworld"; char data [] = str.toCharArray(); for(int i = 0; i < data.length; i++){ data[i] -= 32; //轉(zhuǎn)大寫字母簡(jiǎn)化模式更簡(jiǎn)單 System.out.print(data[i] + "、"); } } }
//字符數(shù)組轉(zhuǎn)化為字符串 public class TestDemo4{ public static void main(String args[]){ String str = "helloworld"; char data [] = str.toCharArray(); for(int i = 0; i < data.length; i++){ data[i] -= 32; //轉(zhuǎn)大寫字母簡(jiǎn)化模式更簡(jiǎn)單 System.out.print(data[i] + "、"); } System.out.println(); System.out.println(new String(data));//字符串?dāng)?shù)組全部轉(zhuǎn)化為字符數(shù)組 System.out.println(new String(data,1,4));//字符串?dāng)?shù)組部分轉(zhuǎn)化為字符數(shù)組 } }
判斷字符串是否由數(shù)字組成
public class TestDemo5{ public static void main(String args[]){ String str1 = "helloworld"; String str = "1234567890"; Judgenum(str); Judgenum(str1); } public static void Judgenum(String str){ char data [] = str.toCharArray(); boolean judge = true; for(int i = 0; i < data.length; i++){ if(data[i]>= '0' && data[i]<= '9'){ judge = false; } } if(judge){ System.out.println(str+"是由字母組成"); }else System.out.println(str+"是由數(shù)字組成"); } }
1.8字節(jié)和字符串
方法名稱 | 類型 | 描述 |
---|---|---|
public String(byte[] bytes) | 構(gòu)造 | 將部分字節(jié)數(shù)組變?yōu)樽址?/td> |
public String(byte[] bytes, int offset,int length) | 構(gòu)造 | 將部分字節(jié)數(shù)組變?yōu)樽址?bytes——要解碼為字符的字節(jié) offset——要解碼的第一個(gè)字節(jié)的索引 length——要解碼的字節(jié)數(shù) |
public byte[] getBytes() | 普通 | 將字符串變?yōu)樽止?jié)數(shù)組 |
public byte[] getBytes(String charsetName) throws UnsupportedEncodingException | 普通 | 編碼轉(zhuǎn)換編碼 |
//將字符串通過(guò)字節(jié)流轉(zhuǎn)化為大寫 public class TestDemo6{ public static void main(String args[]){ String str = "helloworld"; byte data [] = str.getBytes();//字符串轉(zhuǎn)換為字節(jié)數(shù)組 for(int i = 0; i < data.length ; i++){ System.out.print(data[i]+"、"); data[i] -= 32; } System.out.println(new String(data));//字節(jié)數(shù)組轉(zhuǎn)化為字符串 } }
一般情況下,在程序之中如果想要操作字節(jié)數(shù)組只有兩種情況:
**1、**需要進(jìn)行編碼的轉(zhuǎn)化;
2、 數(shù)據(jù)要進(jìn)行傳輸?shù)臅r(shí)候。
**3、**二進(jìn)制文件適合字節(jié)處理
1.9字符串比較
方法名稱 | 類型 | 描述 |
---|---|---|
public boolean equals(String anObject) | 普通 | 區(qū)分大小寫比較 |
public boolean equalsIgnoreCase(String anotherString) | 普通 | 不區(qū)分大小寫比較 |
public int compareTo(String anotherString) | 普通 | 比較兩個(gè)字符串的大小關(guān)系 |
如果現(xiàn)在要比較兩個(gè)字符串的大小關(guān)系,那么就必須使用comepareTo()方法完成,而這個(gè)方法返回int型數(shù)據(jù),而這個(gè)int型數(shù)據(jù)有三種結(jié)果:大于(返回結(jié)果大于0)、小于(返回結(jié)果小于0)、等于(返回結(jié)果為0).
public class CompareTo{ public static void main(String args[]){ String str1 = "HELLO"; String str2= "hello"; System.out.println(str1.compareTo(str2)); } }
1.10字符串查找
方法名稱 | 類型 | 描述 |
---|---|---|
public boolean contains(String s) | 普通 | 判斷一個(gè)子字符串是否村存在 |
public int indexOf(String str) | 普通 | 返回字符串中第一次出現(xiàn)字符串的索引 |
public int indexOf(String str, int fromIndex) | 普通 | 從指定地方開(kāi)始查找子字符串的位置 |
public int lastIndexOf(String str) | 普通 | 從后向前查找子字符串的位置 |
public int lastIndexOf(String str, int fromIndex) | 普通 | 從指定位置由后向前查找 |
public boolean startsWith(String prefix) | 普通 | 從頭判斷是否以某字符串開(kāi)頭 |
public boolean startsWith(String prefix,int toffset) | 普通 | 從指定位置判斷是否以字符串開(kāi)頭 |
public boolean endsWith(String suffix) | 普通 | 判斷以某字符串結(jié)尾 |
public class TestDemo7{ public static void main(String args[]){ String str = "helloworld"; System.out.println(str.contains("world")); //true //使用indexOf()進(jìn)行查找 System.out.println(str.indexOf("world")); System.out.println(str.indexOf("java")); //JDK1,5之前這樣使用 if(str.indexOf() != -1){ System.out.println("可以查找到指定的內(nèi)容"); } } }
- 基本上所有的查找現(xiàn)在都是通過(guò)contains()方法完成。
- 需要注意的是,如果內(nèi)容重復(fù)indexOf()它只能返回查找的第一個(gè)位置。
- 在進(jìn)行查找的時(shí)候往往會(huì)判斷開(kāi)頭或結(jié)尾。
public class TestDemo7{ public static void main(String args[]){ String str = "**@@helloworld##"; System.out.println(str.startsWith("**")); //true System.out.println(str.startsWith("@@",2)); //true System.out.println(str.endsWith("##")); //true } }
1.11字符串的替換
方法名稱 | 類型 | 描述 |
---|---|---|
public String replaceAll(String regex,String replacement) | 普通 | 替換所有的內(nèi)容 |
public String replaceFirst(String regex,String replacement) | 普通 | 替換首內(nèi)容 |
public class TestDemo7{ public static void main(String args[]){ String str = "**@@helloworld##"; System.out.println(str.replaceAll("l","_")); //**@@he__owor_d## } }
1.12字符串的拆分
方法名稱 | 類型 | 描述 |
---|---|---|
public String[] split(String regex) | 普通 | 將字符串全部拆分 |
public String[] split(String regex,int limit) | 普通 | 將字符串部分拆分 |
public class TestDemo8{ public static void main(String args[]){ String str = "hello world hello zsr hello csdn"; String result [] = str.split(" "); //按照空格進(jìn)行拆分 //hello、world、hello、zsr、hello、csdn、 for(int i = 0; i < result.length ; i++){ System.out.print(result[i]+"、"); } System.out.println(); //部分拆分 String result1 [] = str.split(" ",3); //按照空格進(jìn)行拆分 //第二個(gè)參數(shù) 從第幾個(gè)位置開(kāi)始不進(jìn)行拆分操作 //hello、world、hello zsr hello csdn、 for(int i = 0; i < result1.length ; i++){ System.out.print(result1[i]+"、"); } } }
//拆分ip地址 public class TestDemo9{ //吳國(guó)發(fā)現(xiàn)內(nèi)容無(wú)法拆分,就需要用到“\\”進(jìn)行轉(zhuǎn)義 public static void main(String args[]){ //120 //11 //219 //223:57114 String str = "120.11.219.223:57114"; String result [] = str.split("\\."); for(int i = 0 ; i < result.length ; i++){ System.out.println(result[i]); } } }
1.13字符串的截取
方法名稱 | 類型 | 描述 |
---|---|---|
public String substring(int beginIndex) | 普通 | 從指定位置截取到結(jié)尾 |
public String substring(int beginIndex,int endIndex) | 普通 | 截取部分內(nèi)容 |
public class TestDemo10{ public static void main(String args[]){ String str = "helloworld"; //world System.out.println(str.substring(5)); //hello System.out.println(str.substring(0,5)); } }
1.14其他操作方法
方法名稱 | 類型 | 描述 |
---|---|---|
Public String trim() | 普通 | 去掉左右空格,保留中間空格 |
public String toUpperCase() | 普通 | 將全部字符串轉(zhuǎn)大寫 |
public String toLowerCase() | 普通 | 將全部字符串轉(zhuǎn)小寫 |
public String intern() | 普通 | 字符串入對(duì)象池 |
public String concat() | 普通 | 字符串連接 |
思考題:
1.現(xiàn)在給出了如下一個(gè)字符串格式:“姓名:成績(jī)|姓名:成績(jī)|姓名:成績(jī)”,例如:給定的字符串是:“Tom:90|Jerry:80|tony:89”,要求可以對(duì)以上數(shù)據(jù)進(jìn)行處理,將數(shù)據(jù)按照如下的形式顯示:姓名:Tom,成績(jī):90;
public class Exam1{ public static void main(String args[]){ String str = "Tom:90|Jerry:80|tony:89"; String data [] = str.split("\\|"); for(int i = 0 ; i < data.length ; i++){ String result [] = data[i].split(":"); System.out.print("姓名 = " + result[0] + ","); System.out.println("年齡 = " + result[1]); } /*姓名 = Tom,年齡 = 90 姓名 = Jerry,年齡 = 80 姓名 = tony,年齡 = 89*/ } }
2.給定一個(gè)email地址,要求驗(yàn)證其是否正確,提示:可以簡(jiǎn)單的驗(yàn)證一下,重點(diǎn)驗(yàn)證“@”和“.”。標(biāo)準(zhǔn)如下:
1.email長(zhǎng)度不短于5
2.@和.不能做開(kāi)頭或結(jié)尾
3.@和.順序要有定義
public class Exam2{ public static void main(String args[]){ String email = "1016942589.@qqcom"; char date[] = email.toCharArray(); if (date.length>5&&email.startsWith("@")==false && email.startsWith(".")==false && email.endsWith("@")==false &&email.endsWith(".")==false && email.indexOf(".") >email.indexOf("@")) {System.out.println("正確"); }else{System.out.println("錯(cuò)誤");} } }
總結(jié)
本篇文章就到這里了,希望能給你帶來(lái)幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
Java通過(guò)遞歸算法解決迷宮與漢諾塔及八皇后問(wèn)題
方法就是用來(lái)完成解決某件事情或?qū)崿F(xiàn)某個(gè)功能的辦法;程序調(diào)用自身的編程技巧稱為遞歸,本文主要講的是通過(guò)遞歸來(lái)實(shí)現(xiàn)三個(gè)經(jīng)典的問(wèn)題,解決迷宮,漢諾塔,八皇后問(wèn)題,感興趣的朋友可以參考一下2022-05-05JDK8中String的intern()方法實(shí)例詳細(xì)解讀
String字符串在我們?nèi)粘i_(kāi)發(fā)中最常用的,當(dāng)然還有他的兩個(gè)兄弟StringBuilder和StringBuilder,接下來(lái)通過(guò)本文給大家介紹JDK8中String的intern()方法詳細(xì)解讀,需要的朋友可以參考下2022-09-09Java參數(shù)校驗(yàn)詳解之使用@Valid注解和自定義注解進(jìn)行參數(shù)驗(yàn)證
在后端開(kāi)發(fā)中,參數(shù)校驗(yàn)是非常普遍的,下面這篇文章主要給大家介紹了關(guān)于Java參數(shù)校驗(yàn)詳解之使用@Valid注解和自定義注解進(jìn)行參數(shù)驗(yàn)證的相關(guān)資料,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-06-06一篇文章教你如何在SpringCloud項(xiàng)目中使用OpenFeign
這篇文章主要介紹了SpringCloud 使用Open feign 優(yōu)化詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-08-08Spring?Boot+Aop記錄用戶操作日志實(shí)戰(zhàn)記錄
在Spring框架中使用AOP配合自定義注解可以方便的實(shí)現(xiàn)用戶操作的監(jiān)控,下面這篇文章主要給大家介紹了關(guān)于Spring?Boot+Aop記錄用戶操作日志實(shí)戰(zhàn)的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-04-04SpringBoot項(xiàng)目啟動(dòng)打包報(bào)錯(cuò)類文件具有錯(cuò)誤的版本 61.0, 應(yīng)為 52.0的解決
這篇文章主要給大家介紹了關(guān)于SpringBoot項(xiàng)目啟動(dòng)打包報(bào)錯(cuò)類文件具有錯(cuò)誤的版本 61.0, 應(yīng)為 52.0的解決方法,文中有詳細(xì)的排查過(guò)程和解決方法,通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-11-11Java并發(fā)編程示例(六):等待線程執(zhí)行終止
這篇文章主要介紹了Java并發(fā)編程示例(六):等待線程執(zhí)行終止,在本節(jié),示例程序演示等待初始化方法完成后,再去執(zhí)行其他任務(wù),需要的朋友可以參考下2014-12-12SpringBoot使用責(zé)任鏈模式優(yōu)化業(yè)務(wù)邏輯中的if-else代碼
在開(kāi)發(fā)過(guò)程中,我們經(jīng)常會(huì)遇到需要根據(jù)不同的條件執(zhí)行不同的邏輯的情況,我們可以考慮使用責(zé)任鏈模式來(lái)優(yōu)化代碼結(jié)構(gòu),使得代碼更加清晰、可擴(kuò)展和易于維護(hù)2023-06-06Java?Spring框架的注解式開(kāi)發(fā)你了解嗎
這篇文章主要為大家詳細(xì)介紹了Spring框架的注解式,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助2022-03-03java實(shí)現(xiàn)異步線程,回調(diào)接口方式
這篇文章主要介紹了java實(shí)現(xiàn)異步線程,回調(diào)接口方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07