java-String类
?
String類
String類是對(duì)字符串事物的描述,該類定義了專門用于操作字符串的方法。被final修飾,意味不能有子類。
- 字符串是一個(gè)特殊的對(duì)象。
- 字符串一旦初始化就不可以被改變。
例如:String s = new String(“abc”);String s1 = “abc”;構(gòu)造內(nèi)容一樣,后者更常用。s1是一個(gè)類類型變量,字符串”abc”是對(duì)象。不一定非要有new才是對(duì)象。只要是雙引號(hào)內(nèi)的內(nèi)容,都是String類的對(duì)象。
字符串一旦被初始化,就不可以被改變(屬性)。變量s1可以變化,可以指向其他對(duì)象,但是對(duì)象”abc”不會(huì)變化。
String類復(fù)寫了Object類中的equals方法,用于判斷字符串內(nèi)容是否相同,而不是以前的判斷內(nèi)存地址值是否一致。
1 class StringDemo 2 { 3 public static void main(String[] args) 4 { 5 String s1 = "abc";//s1是一個(gè)類類型變量, "abc"是一個(gè)對(duì)象。 6 String s2 = new String("abc"); 7 //s1和s2有什么區(qū)別? 8 //s1在內(nèi)存中有一個(gè)對(duì)象。 9 //s2在內(nèi)存中有兩個(gè)對(duì)象。 10 System.out.println(s1==s2);//比較地址值,false 11 System.out.println(s1.equals(s2));//復(fù)寫了equals方法用來比較字符串內(nèi)容是否相同,true 12 } 13 }String類常見操作字符串方法
1,獲取。
?? ?1.1 字符串中的包含的字符數(shù),也就是字符串的長度。
?? ??? ?int length():獲取長度。
?? ?1.2 根據(jù)位置獲取位置上某個(gè)字符。
?? ??? ?char charAt(int index):
?? ?1.3 根據(jù)字符獲取該字符在字符串中位置。
? ? ? ? int indexOf(int ch):返回的是ch在字符串中第一次出現(xiàn)的位置。
??????? int indexOf(int ch, int fromIndex) :從fromIndex指定位置開始,獲取ch在字符串中出現(xiàn)的位置。
??????? int indexOf(String str):返回的是str在字符串中第一次出現(xiàn)的位置。
??????? int indexOf(String str, int fromIndex) :從fromIndex指定位置開始,獲取str在字符串中出現(xiàn)的位置。
??????? ?
?? ??? ?int lastIndexOf(int ch) :反向索引,返回指定字符在此字符串中最后一次出現(xiàn)處的位置。
?? ??? ?int lastIndexOf(int ch, int fromIndex)
??????? int lastIndexOf(String str)
??????? int lastIndexOf(String str, int fromIndex)
?
1 class StringFunctionDemo 2 { 3 public static void main(String[] args) 4 { 5 String s1 = "cabfhabf"; 6 sop(s1.length());//返回字符串長度-----8 7 sop(s1.charAt(3));//返回角標(biāo)為3的字符---f 8 sop(s1.indexOf('f'));//返回f第一次出現(xiàn)的位置-----3 9 sop(s1.indexOf('f',4));//從角標(biāo)4開始返回f第一次出現(xiàn)的位置-----7 10 sop(s1.indexOf("ab"));//返回字符串"ab"第一次出現(xiàn)的位置--------1 11 sop(s1.indexOf("ab",4));//從角標(biāo)4開始返回字符串"ab"第一次出現(xiàn)的位置-------5 12 13 //反向索引 14 sop(s1.lastIndexOf('f')); 15 sop(s1.lastIndexOf('f',4)); 16 sop(s1.lastIndexOf("ab")); 17 sop(s1.lastIndexOf("ab",4)); 18 19 } 20 public static void sop(Object obj) 21 { 22 System.out.println(obj); 23 } 24 25 } 26?
2,判斷。
?? ?2.1 字符串中是否包含某一個(gè)子串。
?? ??? ?boolean contains(str):
?? ???? 比較:indexOf(str):可以索引str第一次出現(xiàn)位置,如果返回-1.表示該str不在字符串中存在。該方法既可以判斷,也可以獲取出現(xiàn)的位置。
?? ?2.2 字符中是否有內(nèi)容。
?? ??? ?boolean isEmpty(): 原理就是判斷長度是否為0.
?? ?2.3 字符串是否是以指定內(nèi)容開頭。
?? ??? ?boolean startsWith(str);
?? ?2.4 字符串是否是以指定內(nèi)容結(jié)尾。
?? ??? ?boolean endsWith(str);
?? ?2.5 判斷字符串內(nèi)容是否相同。復(fù)寫了Object類中的equals方法。
?? ??? ?boolean equals(str);
?? ?2.6 判斷內(nèi)容是否相同,并忽略大小寫。
?? ??? ?boolean equalsIgnoreCase();
3,轉(zhuǎn)換。
?? ?3.1 將字符數(shù)組轉(zhuǎn)成字符串。
?? ??? ?構(gòu)造函數(shù):String(char[] ch)
?? ??? ??? ??? ?????? String(char[], int offset, int count):將字符數(shù)組中的一部分轉(zhuǎn)成字符串。offset為起始角標(biāo),count為字符個(gè)數(shù)。
?? ??? ?靜態(tài)方法:
?? ??? ??? ??? ?static String copyValueOf(char[]);
?? ??? ??? ??? ?static String copyValueOf(char[] data, int offset, int count)
?? ??? ??? ??? ?static String valueOf(char c):
?? ??? ?
?? ?3.2 將字符串轉(zhuǎn)成字符數(shù)組。
?? ??? ?char[] toCharArray():
?? ?3.3 將字節(jié)數(shù)組轉(zhuǎn)成字符串。
?? ??? ??? ?String(byte[])
?? ??? ??? ?String(byte[],offset,count):將字節(jié)數(shù)組中的一部分轉(zhuǎn)成字符串。
?? ?3.4 將字符串轉(zhuǎn)成字節(jié)數(shù)組。
?? ??? ??? ?byte[]? getBytes():
?? ?3.5 將基本數(shù)據(jù)類型轉(zhuǎn)成字符串。
?? ??? ?static String valueOf(int)
?? ??? ?static String valueOf(double)
?? ??? ?//3+"";//String.valueOf(3);
?????? 特殊:字符串和字節(jié)數(shù)組在轉(zhuǎn)換過程中,是可以指定編碼表的。
4,替換
?? ?String replace(oldchar,newchar);
?
5,切割
?? ?String[]? split(regex);
?
6,子串。獲取字符串中的一部分。
?? ?String substring(begin);
?? ?String substring(begin,end);
?7,轉(zhuǎn)換,去除空格,比較。
?? ?7.1 將字符串轉(zhuǎn)成大寫或則小寫。
?? ??? ? String toUpperCase();
?? ??? ? String toLowerCase();
?? ?7.2 將字符串兩端的多個(gè)空格去除。
?? ??? ?String trim();
?? ?7.3 對(duì)兩個(gè)字符串進(jìn)行自然順序的比較。
?? ??? ?int compareTo(string);
??????? int compareToIgnoreCase(string);
?
1 class StringFunctionDemo 2 { 3 public static void main(String[] args) 4 { 5 String s = " Hello Java "; 6 sop(s.toLowerCase()); 7 sop(s.toUpperCase()); 8 sop(s.trim()); 9 10 String s1 = "a1c"; 11 String s2 = "aaa"; 12 13 sop(s1.compareTo(s2)); 14 15 } 16 public static void sop(Object obj) 17 { 18 System.out.println(obj); 19 } 20 } 21 ---------- java ---------- 22 hello java 23 HELLO JAVA 24 Hello Java 25 -48 26 27 輸出完成 (耗時(shí) 0 秒) - 正常終止?
StringBuffer類
StringBuffer是字符串緩沖區(qū)。
是一個(gè)容器。
特點(diǎn):
1,長度是可變化的。
2,可以字節(jié)操作多個(gè)數(shù)據(jù)類型。
3,最終會(huì)通過toString方法變成字符串。
字符串的組成原理就是通過該類實(shí)現(xiàn)的。
StringBuffer可以對(duì)字符串內(nèi)容進(jìn)行增刪。
1,存儲(chǔ)。
?? ?StringBuffer append():將指定數(shù)據(jù)作為參數(shù)添加到已有數(shù)據(jù)結(jié)尾處。
?? ?StringBuffer insert(index,數(shù)據(jù)):可以將數(shù)據(jù)插入到指定index位置。
?
2,刪除。
?? ?StringBuffer delete(start,end):刪除緩沖區(qū)中的數(shù)據(jù),包含start,不包含end。
?? ?StringBuffer deleteCharAt(index):刪除指定位置的字符。
?
3,獲取。
?? ?char charAt(int index)
?? ?int indexOf(String str)
?? ?int lastIndexOf(String str)
?? ?int length()
?? ?String substring(int start, int end)
?4,修改。
?? ?StringBuffer replace(start,end,string);
?? ?void setCharAt(int index, char ch) ;
?5,反轉(zhuǎn)。
?? ?StringBuffer reverse();
6,
?? ?將緩沖區(qū)中指定數(shù)據(jù)存儲(chǔ)到指定字符數(shù)組中。
?? ?void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
?
JDK1.5 版本之后出現(xiàn)了StringBuilder.
StringBuffer是線程同步。
StringBuilder是線程不同步。
開發(fā)建議使用StringBuilder.
基本數(shù)據(jù)類型對(duì)象包裝類
將基本數(shù)據(jù)類型封裝成對(duì)象的好處在于可以在 對(duì)象中定義更多的功能方法操作該數(shù)據(jù)。
byte?? ??? Byte
short?? ?? Short
int?? ??? ?? Integer
long?? ???? Long
boolean?? Boolean
float?? ???? Float
double?? ?Double
char?? ??? Character
基本數(shù)據(jù)類型轉(zhuǎn)成字符串。
- ?? ?基本數(shù)據(jù)類型+""
- ??? 基本數(shù)據(jù)類型.toString(基本數(shù)據(jù)類型值); 如: Integer.toString(34);//將34整數(shù)變成"34";
字符串轉(zhuǎn)成基本數(shù)據(jù)類型。
?? ?xxx a = Xxx.parseXxx(String);
??? //靜態(tài)轉(zhuǎn)換方法
?? ?int a = Integer.parseInt("123");
?? ?double b = Double.parseDouble("12.23");
?? ?boolean b = Boolean.parseBoolean("true");
??? //動(dòng)態(tài)方法,對(duì)象調(diào)用
??? Integer i = new Integer("123");
?? ?int num = i.intValue();
十進(jìn)制轉(zhuǎn)成其他進(jìn)制:
???????? toBinaryString();轉(zhuǎn)二進(jìn)制
???????? toOctalString();轉(zhuǎn)八進(jìn)制
???????? toHexString();轉(zhuǎn)十六進(jìn)制??????????????????????????????????????
其他進(jìn)制轉(zhuǎn)成十進(jìn)制:
???????? static int parseInt(String s, int index);第二個(gè)參數(shù)代表進(jìn)制,將某個(gè)進(jìn)制的某個(gè)字符串轉(zhuǎn)成十進(jìn)制的基本數(shù)據(jù)。例如Integer.parseInt("110",2);將二進(jìn)制的110轉(zhuǎn)成十進(jìn)制。
基本數(shù)據(jù)類型對(duì)象包裝類新特性?
JDK1.5以后,簡化了定義方式。
?Integer x = new Integer(4);可以直接寫成
?Integer x = 4;//自動(dòng)裝箱。
?x = x + 5;//自動(dòng)拆箱。通過intValue方法。
需要注意:
?在使用時(shí),Integer x = null;上面的代碼就會(huì)出現(xiàn)NullPointerException。
?
Integer m = 128;
Integer n = 128;
sop("m==n:"+(m==n));結(jié)果為flase.
Integer a = 127;
Integer b = 127;
sop("a==b:"+(a==b));//結(jié)果為true。
當(dāng)數(shù)值在byte范圍內(nèi)容,對(duì)于新特性,如果該數(shù)值已經(jīng)存在,則不會(huì)在開辟新的空間。
int compareTo(Integer anotherInteger)比較對(duì)象內(nèi)的數(shù)值大小。小于返回-1;等于返回0;大于返回1。
?
轉(zhuǎn)載于:https://www.cnblogs.com/xscn/p/3221945.html
總結(jié)
以上是生活随笔為你收集整理的java-String类的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Docker-compose配置Mysq
- 下一篇: Drools规则引擎使用