Java String函數(shù)的使用詳細(xì)介紹
String
str本來(lái)是字符串常量的引用,應(yīng)該打印地址,但是編譯器重寫(xiě)了toString方法,所以打印hello

String 的構(gòu)造方法
public class test {
public static void main(String[] args) {
// 用常量字符串構(gòu)造
String s1 = "hello";
System.out.println(s1);
// 用字符數(shù)組構(gòu)造
char[] array = new char[]{'a','b','c'};
String s2 = new String(array);
System.out.println(s2);
char[] array1 = new char[]{'a','b','c'};
String s4 = new String(array,0,2);
// 從0位置后拿2個(gè)字符
System.out.println(s4);
// 直接newSting對(duì)象構(gòu)造
String s3 = new String("hello");
System.out.println(s3);
}
}String對(duì)象在內(nèi)存中的情況


4. 空指針異常和空字符串,isEmpty()判斷是否為空字符串

字符串比
- s1 == s2 比較地址
- s1.equals(s2)比較是否相等,返回true或者false
- s1.compareTo(s2)比較大小
- s1.compareToIgonreCase(s2)忽略大小寫(xiě)比較
public class test {
public static void main(String[] args) {
String s1 = new String("hello");
String s2 = new String("Hello");
System.out.println(s1 == s2);
// 不等于,s1和s2表示對(duì)象的引用都存的是地址
System.out.println(s1.equals(s2));
System.out.println(s1.compareTo(s2));
// s1 大于 s2 返回正數(shù)
// s1 小于 s2 返回負(fù)數(shù)
// s1 等于 s2 返回0
System.out.println(s1.compareToIgnoreCase(s2));
// 忽略大小寫(xiě)比較
}
}字符串查找
char charAt(int index),返回?cái)?shù)組中下標(biāo)對(duì)應(yīng)的字符
public static void main(String[] args) {
String s1 = new String("hello");
char ch = s1.charAt(1);
System.out.println(ch);// e
}int indexOf(char ch),返回第一次出現(xiàn)ch字符的下標(biāo)
String s2 = new String("hello");
int index = s1.indexOf('l');
System.out.println(index);// 2int indexOf(char ch,int k),k表示下標(biāo),從指定位置開(kāi)始查找
String s2 = new String("hello");
int index = s1.indexOf('l',3);
System.out.println(index);// 3int indexOf(String s),可以查找子串在主串中出現(xiàn)的位置,如果沒(méi)有找到返回-1
String s3 = "ababcdeabcf";
int index = s3.indexOf("abc");
System.out.println(index);// 2
int index1 = s3.indexOf("abc",3);
System.out.println(index1);// 7 int lastIndexOf(String s),倒著往前找,返回第一個(gè)找到的下標(biāo)
String s3 = "ababcabcd";
int index = s3.lastIndexOf("abc");
System.out.println(index);// 5
int index1 = s3.lastIndexOf("abc",4);
System.out.println(index1);// 2
// 從4下標(biāo)位置倒著往前找轉(zhuǎn)化
數(shù)字和字符串之間的轉(zhuǎn)化
String s2 = String.valueOf(new Student("zhangsan",20));
System.out.println(s2);
String s3 = String.valueOf(123);
System.out.println(s3);
String s4 = String.valueOf(123.34);
System.out.println(s4);
String s5 = String.valueOf(true);
System.out.println(s5);// true
2. 字符串轉(zhuǎn)數(shù)字
int a = Integer.parseInt("190");
System.out.println(a);
int b = Integer.parseInt("19.9");
// 錯(cuò)誤,給的要是整數(shù)的字符串
大小寫(xiě)轉(zhuǎn)化
小寫(xiě)轉(zhuǎn)大寫(xiě):toUpperCase
轉(zhuǎn)變?yōu)榇髮?xiě)不是在原有字符串的基礎(chǔ)上轉(zhuǎn)換,而是轉(zhuǎn)變?yōu)榇髮?xiě)是一個(gè)新的對(duì)象,不會(huì)改變?cè)械淖址?/p>
String s = "hello";
String ret = s.toUpperCase();
System.out.println(ret);大寫(xiě)轉(zhuǎn)小寫(xiě)
String s = "HEllo";
String ret = s.toLowerCase();
System.out.println(ret);// hello字符串轉(zhuǎn)為數(shù)組
String s = "hello"; char[] ret = s.toCharArray(); System.out.println(Arrays.toString(ret)); // [h,e,l,l,o]
數(shù)組轉(zhuǎn)為字符串
char[] ret = {'a','b','c'};
String s = new String(ret);
System.out.println(s);// abc格式轉(zhuǎn)換
String s = String.format("%d-%d-%d",2019,9,20);
System.out.println(s);// 2019-9-20字符串替換
替換字符串

替換單個(gè)字符

3. 替換第一個(gè)ab

4. 把所有的ab都替換為123

字符串拆分
s.split()以這里面的字符串為標(biāo)準(zhǔn)分割
public static void main(String[] args) {
String s = "hello world k";
String[] array = s.split(" ");
System.out.println(Arrays.toString(array));
// [hello,world,k]
String s1 = "hello world k";
String[] array1 = s.split(" ",2);
// 以空格分割最多分成兩組
System.out.println(Arrays.toString(array1));
// [hello, world k]
}特殊的情況,使用轉(zhuǎn)義字符




多次分割

字符串截?。ǔS茫?/h3>
substring(a,b) [a,b) a和b均為下標(biāo)
String str = "hello"; String ret = str.substring(0,3);// hel
給一個(gè)參數(shù),會(huì)把后面的全不截取
String str = "hello"; String ret = str.substring(0);// hello
trim(),去掉字符串的左右空格,中間的空格不可去掉

字符串的不可變性
- 字符串中的value[] 數(shù)組是被private修飾的,也沒(méi)有提供get方法,在類(lèi)外是無(wú)法拿到的,就無(wú)法修改該數(shù)組了
- 而被final修飾的,只是表明它是常量了,它的引用只能指向一個(gè)對(duì)象,不能被改變成指向別的對(duì)象
- 被final修飾的不能被繼承
final int[] array = {1,2,3};
// array = new int[]{1,2};
// 不能改變array的指向了
array[0] = 2;// 可被修改
到此這篇關(guān)于Java String函數(shù)的使用的文章就介紹到這了,更多相關(guān)Java String函數(shù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
一文搞懂SpringBoot如何利用@Async實(shí)現(xiàn)異步調(diào)用
異步調(diào)用幾乎是處理高并發(fā),解決性能問(wèn)題常用的手段,如何開(kāi)啟異步調(diào)用?SpringBoot中提供了非常簡(jiǎn)單的方式,就是一個(gè)注解@Async。今天我們重新認(rèn)識(shí)一下@Async,以及注意事項(xiàng)2022-09-09
Vue3源碼解讀effectScope API及實(shí)現(xiàn)原理
這篇文章主要為大家介紹了Vue3源碼解讀effectScope API及實(shí)現(xiàn)原理,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
idea中springboot整合mybatis找不到mapper接口的原因分析
這篇文章主要介紹了idea中springboot整合mybatis找不到mapper接口的原因分析及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01
詳解SpringBoot 添加對(duì)JSP的支持(附常見(jiàn)坑點(diǎn))
這篇文章主要介紹了詳解SpringBoot 添加對(duì)JSP的支持(附常見(jiàn)坑點(diǎn)),非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-10-10
Java實(shí)現(xiàn)學(xué)生管理系統(tǒng)詳解
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)學(xué)生管理系統(tǒng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助2021-10-10
基于接口實(shí)現(xiàn)java動(dòng)態(tài)代理示例
這篇文章主要介紹了基于接口實(shí)現(xiàn)java動(dòng)態(tài)代理示例,需要的朋友可以參考下2014-04-04
Springbootadmin與security沖突問(wèn)題及解決
這篇文章主要介紹了Springbootadmin與security沖突問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08
Java語(yǔ)法基礎(chǔ)之函數(shù)的使用說(shuō)明
函數(shù)就是定義在類(lèi)中的具有特定功能的一段小程序,函數(shù)也稱(chēng)為方法2013-07-07

