日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java-String类

發(fā)布時(shí)間:2023/12/10 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java-String类 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

?

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();

1 class StringFunctionDemo 2 { 3 public static void main(String[] args) 4 { 5 String str = "ArrayDemo.java"; 6 String str1 = "arraydemo.java"; 7 //判斷字符串中是否有內(nèi)容 8 sop(str.isEmpty()); 9 //判斷文件名稱是否是Array單詞開頭。 10 sop(str.startsWith("Array")); 11 //判斷文件名稱是否是.java的文件。 12 sop(str.endsWith(".java")); 13 //判斷文件名中是否包含Demo 14 sop(str.contains("Demo"));//不要用contains判斷文件類型,比如hehe.java.txt 15 16 //將此String與另一個(gè)String比較,不考慮大小寫。常用于登陸時(shí)用戶名校驗(yàn)。 17 sop(str.equalsIgnoreCase(str1)); 18 } 19 public static void sop(Object obj) 20 { 21 System.out.println(obj); 22 } 23 24 } 25

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)換過程中,是可以指定編碼表的。

1 class StringFunctionDemo 2 { 3 public static void main(String[] args) 4 { 5 //將字符數(shù)組轉(zhuǎn)成字符串,通過構(gòu)造函數(shù)。 6 char[] arr = {'h','e','l','l','o','j','a','v','a'}; 7 String str =new String(arr); 8 sop("str="+str); 9 String str1 =new String(arr,5,4); 10 sop("str1="+str1); 11 //通過靜態(tài)方法。 12 sop(String.copyValueOf(arr)); 13 sop(String.copyValueOf(arr,5,4)); 14 15 sop(String.valueOf('x'));//單個(gè)字符轉(zhuǎn)換成字符串 16 sop(String.valueOf(3.14159));//基本數(shù)據(jù)類型(單個(gè))轉(zhuǎn)換成字符串 17 18 //將字符串轉(zhuǎn)成字符數(shù)組。 19 char[] arr1 = str.toCharArray(); 20 for(int x=0; x<arr1.length; x++) 21 { 22 sop("arr["+x+"]="+arr1[x]); 23 } 24 } 25 public static void sop(Object obj) 26 { 27 System.out.println(obj); 28 } 29 30 } 31 ---------- java ---------- 32 str=hellojava 33 str1=java 34 hellojava 35 java 36 x 37 3.14159 38 arr[0]=h 39 arr[1]=e 40 arr[2]=l 41 arr[3]=l 42 arr[4]=o 43 arr[5]=j 44 arr[6]=a 45 arr[7]=v 46 arr[8]=a 47 48 輸出完成 (耗時(shí) 0 秒) - 正常終止

4,替換
?? ?String replace(oldchar,newchar);

1 class StringFunctionDemo 2 { 3 public static void main(String[] args) 4 { 5 String s = "hello java"; 6 7 String s1 = s.replace('a','n');//如果要替換的字符不存在,返回的還是原串。 8 9 String s2 = s.replace("java","world"); 10 11 sop("s="+s); 12 sop("s1="+s1); 13 sop("s2="+s2); 14 15 } 16 public static void sop(Object obj) 17 { 18 System.out.println(obj); 19 } 20 21 } 22 ---------- java ---------- 23 s=hello java 24 s1=hello jnvn 25 s2=hello world 26 27 輸出完成 (耗時(shí) 0 秒) - 正常終止

?

5,切割
?? ?String[]? split(regex);

1 class StringFunctionDemo 2 { 3 public static void main(String[] args) 4 { 5 String s = "zhagnsa,lisi,wangwu"; 6 7 String[] arr = s.split(","); 8 9 for(int x = 0; x<arr.length; x++) 10 { 11 sop("arr["+x+"]="+arr[x]); 12 } 13 14 } 15 public static void sop(Object obj) 16 { 17 System.out.println(obj); 18 } 19 20 } 21 ---------- java ---------- 22 arr[0]=zhagnsa 23 arr[1]=lisi 24 arr[2]=wangwu 25 26 輸出完成 (耗時(shí) 0 秒) - 正常終止

?

6,子串。獲取字符串中的一部分。
?? ?String substring(begin);
?? ?String substring(begin,end);

1 class StringFunctionDemo 2 { 3 public static void main(String[] args) 4 { 5 String s = "abcdef"; 6 7 sop(s.substring(2));//從指定位置開始到結(jié)尾。如果角標(biāo)不存在,會(huì)出現(xiàn)字符串角標(biāo)越界異常。 8 sop(s.substring(2,4));//包含頭,不包含尾。s.substring(0,s.length()) 9 sop(s.substring(0,s.length())); 10 11 } 12 public static void sop(Object obj) 13 { 14 System.out.println(obj); 15 } 16 17 } 18 ---------- java ---------- 19 cdef 20 cd 21 abcdef 22 23 輸出完成 (耗時(shí) 0 秒) - 正常終止

?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位置。

1 class StringBufferDemo 2 { 3 public static void main(String[] args) 4 { 5 StringBuffer sb = new StringBuffer(); 6 7 sb.append("hello").append("java").insert(5,','); 8 sop(sb.toString()); 9 10 } 11 public static void sop(Object obj) 12 { 13 System.out.println(obj); 14 } 15 } 16 ---------- java ---------- 17 hello,java 18 19 輸出完成 (耗時(shí) 0 秒) - 正常終止

?

2,刪除。
?? ?StringBuffer delete(start,end):刪除緩沖區(qū)中的數(shù)據(jù),包含start,不包含end。
?? ?StringBuffer deleteCharAt(index):刪除指定位置的字符。

1 class StringBufferDemo 2 { 3 public static void main(String[] args) 4 { 5 StringBuffer sb= new StringBuffer("hello,java"); 6 sb.delete(0,5);//刪除緩沖區(qū)中的數(shù)據(jù),包含start,不包含end。 7 //sb.delete(1,1);//指定位置一樣,不會(huì)刪除 8 //sb.deleteCharAt(5);//刪除指定位置的字符。 9 //sb.delete(0,sb.length());//清空緩沖區(qū)。 10 sop(sb.toString()); 11 12 } 13 public static void sop(Object obj) 14 { 15 System.out.println(obj); 16 } 17 }

?

3,獲取。
?? ?char charAt(int index)
?? ?int indexOf(String str)
?? ?int lastIndexOf(String str)
?? ?int length()
?? ?String substring(int start, int end)

1 class StringBufferDemo 2 { 3 public static void main(String[] args) 4 { 5 StringBuffer sb= new StringBuffer("hello,java"); 6 7 sop(sb.charAt(1)); 8 sop(sb.indexOf("l")); 9 sop(sb.lastIndexOf("l")); 10 sop(sb.length()); 11 sop(sb.substring(0,5)); 12 13 } 14 public static void sop(Object obj) 15 { 16 System.out.println(obj); 17 } 18 } 19 ---------- java ---------- 20 e 21 2 22 3 23 10 24 hello 25 26 輸出完成 (耗時(shí) 0 秒) - 正常終止

?4,修改。
?? ?StringBuffer replace(start,end,string);
?? ?void setCharAt(int index, char ch) ;

1 class StringBufferDemo 2 { 3 public static void main(String[] args) 4 { 5 StringBuffer sb= new StringBuffer("hello,java"); 6 7 sop(sb.replace(0,5,"haha")); 8 sb.setCharAt(4,'+') ; 9 sop(sb.toString()); 10 11 } 12 public static void sop(Object obj) 13 { 14 System.out.println(obj); 15 } 16 } 17 ---------- java ---------- 18 haha,java 19 haha+java 20 21 輸出完成 (耗時(shí) 0 秒) - 正常終止

?5,反轉(zhuǎn)。
?? ?StringBuffer reverse();

1 class StringBufferDemo 2 { 3 public static void main(String[] args) 4 { 5 6 StringBuffer sb= new StringBuffer("hello,java"); 7 sop(sb.reverse()); 8 9 } 10 public static void sop(Object obj) 11 { 12 System.out.println(obj); 13 } 14 } 15 ---------- java ---------- 16 avaj,olleh 17 18 輸出完成 (耗時(shí) 0 秒) - 正常終止

6,
?? ?將緩沖區(qū)中指定數(shù)據(jù)存儲(chǔ)到指定字符數(shù)組中。
?? ?void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)

1 class StringBufferDemo 2 { 3 public static void main(String[] args) 4 { 5 6 StringBuilder sb = new StringBuilder("abcdef"); 7 8 char[] chs = new char[6]; 9 //將緩沖區(qū)中指定數(shù)據(jù)存儲(chǔ)到指定字符數(shù)組中。 10 sb.getChars(1,4,chs,1); 11 12 for(int x=0; x<chs.length; x++) 13 { 14 sop("chs["+x+"]="+chs[x]+";"); 15 } 16 } 17 public static void sop(Object obj) 18 { 19 System.out.println(obj); 20 } 21 } 22 ---------- java ---------- 23 chs[0]= ; 24 chs[1]=b; 25 chs[2]=c; 26 chs[3]=d; 27 chs[4]= ; 28 chs[5]= ; 29 輸出完成 (耗時(shí) 0 秒)

?

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)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。