Java實(shí)戰(zhàn)開發(fā)篇-7 基礎(chǔ)類庫(kù)

基礎(chǔ)類庫(kù)

一、String類

String類是定義不可變的字符串,如

String a= "abc"http://沒(méi)有直接操作字符串的增加刪除改變的方法

用String類創(chuàng)建常量字符串以及相關(guān)子類

(1)直接創(chuàng)建 String a = “abc”
(2)構(gòu)造方法創(chuàng)建 String b = new String(abc);
注:new產(chǎn)生的對(duì)象在堆區(qū)
(3)比較字符串的方法
①equals方法:判斷字符串的內(nèi)容是否相同
②==方法:比較的是對(duì)象本身
(4)compareTo: 獲取大小關(guān)系(比較ASCII碼)
(5)length:獲取字符串長(zhǎng)度
(6)獲取i對(duì)應(yīng)的字符:charAt(i)
注:String類是被final修飾的,不能被繼承
(7)判斷字符串是不是以某個(gè)字符串開頭

String url = "http://www.baidu.com";
System.out.println(url.startWith("http")

(8)判斷字符串是不是以某個(gè)字符串結(jié)尾:endsWith

String pic ="http://www.baidu.com/image/1.jpg"
if(pic.endsWith(“jpg”)){
System.out.println(“圖片”);
}

(9)獲取子字符串:subString(int a ,int b)
注:括號(hào)內(nèi)參數(shù)代表在第a到第b個(gè)字符之間獲取子字符串
(10)替換某個(gè)字符串:replaceAll(String a,String b)
注:用b替換a
(11)將字符串末尾的空格去掉:trim()

二、StringBuilder類以及相關(guān)子類

StringBuilder類定義可變的字符串

用StringBuilder修飾可變字符串

(1)拼接字符串:append
(2)替換字符串:replace
(3)刪除a到b之間的字符串:delete(int a,int b)

三、提取時(shí)間的幾種方法

1.Date類

Date date = new Date();
System.out.pritln(date);

調(diào)整日期格式:SimpleDateFormat

Date date = new Date();
String format = "HH:mm";
SimpleDateFormat dateformat = new SimpleDateFormat(format);
String str = dateformat.format(date);
System.out.pritln(str);

2.Calendar類

Calendar calendar = Calendar.getinstance();
Calendar.get(Canlendar.YEAR);
Calendar.get(Canlendar.MONTH);
Calendar.get(Canlendar.DAY);
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容