Java中String類常用方法使用詳解
一、length()
返回此字符串的長度

public static void main4(String[] args) {
//length()方法
String r = "woyaojindachang";
int length = r.length();
System.out.println(length);
}
這里length返回的是"woyaojindachang"的長度,應(yīng)該是15個字符


二、equals
將此字符串與指定對象進行比較

public static void main(String[] args) {
//equals方法
String r = "woyaojindachang";
if(r.equals("woyaojindachang")) {
System.out.println("字符串相等");
} else {
System.out.println("字符串不同");
}
}
這里的equals返回值是boolean,如果相等返回true,否則返回false

三、charAt()
返回 char指定索引處的值

public static void main(String[] args) {
//charAt
String s = "woyaojindachang";
char s1 = s.charAt(5);
System.out.println(s1);
}
charAt()返回指定處的值,從0開始,5處是j.

四、indexOf()
返回指定字符第一次出現(xiàn)的字符串內(nèi)的索引

public static void main(String[] args) {
//indexOf
String s = "woyaojindachang";
int location = s.indexOf("j");
System.out.println(location);
}
這里返回的是j第一次出現(xiàn)的位置,從0開始,返回5

五、trim()
返回一個字符串,其值為此字符串,并刪除任何前導(dǎo)和尾隨空格

public static void main(String[] args) {
//trim
String s = " wo ";
String s1 = s.trim();
System.out.println(s1);
}
trim去掉wo前面的空格和后面的空格.

六、compareTo()
按字典順序比較兩個字符串

public static void main(String[] args) {
//compareTo
String s = "woyaojindacahng";
int s1 = s.compareTo("woyao");
System.out.println(s1);
}
若調(diào)用該方法的字符串大于參數(shù)字符串,則返回大于0的值, 若相等,則返回數(shù)0, 若小于參數(shù)字符串,則返回小于0的值

七、toLowerCase()
將字符串中的所有字符都轉(zhuǎn)換為小寫字符

public static void main(String[] args) {
//toLowerCase
String s = "WOYAOJINDACHANG";
String s1 = s.toLowerCase();
System.out.println(s1);
}

八、toUpperCase()
將字符串中的所有字符都轉(zhuǎn)換為大寫字符

public static void main(String[] args) {
//toUpperCase
String s = "woyaojindachang";
String s1 = s.toUpperCase();
System.out.println(s1);
}

九、replace()
將此字符串與指定對象進行比較

public static void main(String[] args) {
//replace的使用
System.out.println("將日期中的-替換為.");
String date = "2022-07-30";
System.out.println("替換前: "+date);
String replace = date.replace("-",".");
System.out.println("替換后: "+replace);
}
將2022-07-30中的-全部換成.

十、substring(int beginIndex)
返回字符串中從beginIndex開始的子串

public static void main(String[] args) {
//substring
String s = "woyaojindachang";
String s1 = s.substring(5);
System.out.println(s1);
}
截取從第五位(j)開始的字符串

十一、substring(int beginIndex, int endIndex)
返回從beginIndex開始到endIndex-1的子串

public static void main(String[] args) {
//substring字符串截取
String testDate = "20220730";
String year = testDate.substring(0,4);
System.out.println(year);
String month = testDate.substring(4,6);
System.out.println(month);
String day = testDate.substring(6,8);
System.out.println(day);
System.out.println(year+"年"+month+"月"+day+"日");
}
輸入一個日期,分別截取年月日

總結(jié)
今天向大家介紹了String類的一些常用方法,大家可以去使用一下
以上就是Java中String類常用方法使用詳解的詳細內(nèi)容,更多關(guān)于Java String類的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
SpringBoot如何實現(xiàn)定時任務(wù)示例詳解
使用定時任務(wù)完成一些業(yè)務(wù)邏輯,比如天氣接口的數(shù)據(jù)獲取,定時發(fā)送短信,郵件。以及商城中每天用戶的限額,定時自動收貨等等,這篇文章主要給大家介紹了關(guān)于SpringBoot如何實現(xiàn)定時任務(wù)的相關(guān)資料,需要的朋友可以參考下2021-10-10
淺談為什么阿里巴巴要禁用Executors創(chuàng)建線程池
這篇文章主要介紹了淺談為什么阿里巴巴要禁用Executors創(chuàng)建線程池,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02
詳解如何使用IntelliJ IDEA新建一個Servlet項目
這篇文章主要介紹了詳解如何使用IntelliJ IDEA新建一個Servlet項目,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-11-11
SpringBoot整合SpringDataRedis的示例代碼
這篇文章主要介紹了SpringBoot整合SpringDataRedis的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05
Spring Boot jar可執(zhí)行原理的徹底分析
這篇文章主要給大家介紹了關(guān)于Spring Boot jar可執(zhí)行原理的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家學(xué)習(xí)或者使用Spring Boot具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07

