溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

String類的常用方法有哪些

發(fā)布時(shí)間:2021-10-13 10:54:18 來(lái)源:億速云 閱讀:132 作者:iii 欄目:編程語(yǔ)言

本篇內(nèi)容介紹了“String類的常用方法有哪些”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

String類


  • 字符串是常量,創(chuàng)建之后不可改變

  • 字符串字面值存儲(chǔ)在字符串池中,可以共享。

  • String s = "Hello";產(chǎn)生一個(gè)對(duì)象,字符串池存儲(chǔ)

  • String s = new String ("Hello");//產(chǎn)生兩個(gè)對(duì)象,堆,池各存儲(chǔ)一個(gè)

常用方法

  • public int length():返回字符串長(zhǎng)度

String str = "java是最好的語(yǔ)言";
System.out.println(str.length());//獲取字符串長(zhǎng)度
  • public char charAt(int index):根據(jù)下標(biāo)獲取字符

String str = "java是世界上最好的語(yǔ)言";
System.out.println(str.charAt(str.length()-1));//獲取字符串最后一位
  • public boolean contains(String str):判斷當(dāng)前字符串中是否包含str

String str = "java是世界上最好的語(yǔ)言";
System.out.println(str.contains("java"));//查看字符串中是否包含"java".
  • public char[] toStringArray():將字符串轉(zhuǎn)換成數(shù)組

  • public int indexOf(String str):查找str首次出現(xiàn)的下標(biāo),存在,則返回該下標(biāo);不存在,則返回-1。

  • public int lastIndexOf(String str):查找字符串在當(dāng)前字符串中最后一次出現(xiàn)的下標(biāo)索引

  • public String trim():去掉字符串前后的空格

  • public String toUpperCase():將小寫(xiě)轉(zhuǎn)換成大寫(xiě)

  • public boolean endWith(String str):判斷字符串是否以str結(jié)尾

  • public boolean startWith(String str):判斷字符串是否以str開(kāi)頭

  • public String replace(char oldChar,char newChar);將舊字符串替換成新字符串

  • public String[] split(String str):根據(jù)str做拆分

String say = "java is the best progaming language,java xiang";
String[] arr = say.split("[ ,]+");
System.out.println(arr.length);
for(String string : arr){
    System.out.println(string);
}
  • public String subString():對(duì)字符串進(jìn)行截取

可變字符串

  • StringBuffer:可變長(zhǎng)字符串,JDK1.0提供了,運(yùn)行效率慢、線程安全

  • StringBuilder:可變長(zhǎng)字符串,JDK5.0提供,運(yùn)行效率快、線程不安全。

  1. append();追加 :在字符串末尾追加

  2. insert();添加 :在指定位置添加

  3. replace();替換 :在指定位置替換

  4. delete(); :刪除

public class TestDemo01 {
    public static void main(String [] args){
        StringBuffer sb = new StringBuffer();
        //1 append();追加
        sb.append("java世界第一");
        System.out.println(sb.toString());
        sb.append("真香");
        System.out.println(sb.toString());
        //2 insert();添加
        sb.insert(0,"前面:");
        System.out.println(sb.toString());
        
        sb.replace(0,3,"開(kāi)始:");//替換
        System.out.println(sb.toString());
        
        sb.delete(0,3);//刪除
        System.out.println(sb.toString());
    }

}
  • StringBuilder效率高于StringBuffer

public class TestDemo02 {
    public static void main(String [] args){

        long start = System.currentTimeMillis();
        StringBuffer sb = new StringBuffer();
        for(int i = 0; i < 99999; i++){
            sb.append(i);
        }
        long end = System.currentTimeMillis();
        System.out.println("StringBuffer用時(shí):"+(end - start));

        System.out.println("=============================");
        long start1 = System.currentTimeMillis();
        StringBuilder sb1 = new StringBuilder();
        for (int i = 0; i < 99999; i++) {
            sb1.append(i);
        }
        long end1 = System.currentTimeMillis();
        System.out.println("StringBuilder用時(shí):"+(end1 - start1));

        if((end-start)>(end1-start1)){
            System.out.println("StringBuilder效率高于StringBuffer");
        }else{
            System.out.println("StringBuffer效率高于StringBuilder");
        }


    }
}

BigDecimal

  • 很多實(shí)際應(yīng)用中需要精確 運(yùn)算,而double是近似值存儲(chǔ),不在符合要求,需要借助BigDecimal

  • 位置:java.math包中

  • 作用:精確計(jì)算浮點(diǎn)數(shù)

  • 創(chuàng)建方式:BigDecimal bd = new BigDecimal("1.0");

  • 方法:

  1. BigDecimal add(BigDecimal bd) 加

  2. BigDecimal subtract(BigDecimal bd) 減

  3. BigDecimal multiply(BigDecimal bd) 乘

  4. BigDecimal divide(BigDecimal bd) 除

import java.math.BigDecimal;

public class TestDemo03 {
    public static void main(String[] args) {
        double d1 = 1.0;
        double d2 = 0.9;
        System.out.println(d1 - d2);

        //BigDecimal,大的浮點(diǎn)數(shù)精確計(jì)算
        BigDecimal bd1 = new BigDecimal("1.0");
        BigDecimal bd2 = new BigDecimal("0.9");
        //減法 subtract();
        BigDecimal r1 = bd1.subtract(bd2);
        System.out.println(r1);

        //加法 add();
        BigDecimal r2 = bd1.add(bd2);
        System.out.println(r2);

        //乘法 multiply();
        BigDecimal r3 = bd1.multiply(bd2);
        System.out.println(r3);

        //除法 divide();
        BigDecimal r4 = new BigDecimal("1.4").subtract(new BigDecimal("0.5")).divide(new BigDecimal("0.9"));
        //(1.4 - 0.5)/0.9
        System.out.println(r4);
    }
}

Date

  • Date表示特定的瞬間,精確到毫秒。Date類中的大部分方法都已經(jīng)被Calendar類中的方法所取代

  • 時(shí)間單位

  1. 1秒 = 1000毫秒

  2. 1毫秒 = 1000微秒

  3. 1 微秒 = 1000納秒

Calendar

  • Calendar提供了獲取或設(shè)置各種日歷字段的方法

  • 構(gòu)造方法

protected Calendar() :由于修飾符是protected,所以無(wú)法直接創(chuàng)建該對(duì)象

  • 其他方法

方法名說(shuō)明
static Calendar getInstance()使用默認(rèn)時(shí)區(qū)和區(qū)域獲取日歷
void set (int year,int month,int date,int hourofday,int minute,int second)設(shè)置日歷的年、月、日、時(shí)、分、秒
int get(int field)返回給定日歷字段的值、字段比如年、月、日等
void setTime(Date date)用給定的Date設(shè)置此日歷的時(shí)間。Date-Calendar
Date getTime()返回一個(gè)Date表示此日歷的時(shí)間。Calendar-Date
void add(int field, int amount)按照日歷的規(guī)則,給指定字段添加或減少時(shí)間量
long getTimeInMillies()毫秒為單位返回該日歷的時(shí)間值

SimpleDateFormat

  • SimpleDateFormat是一個(gè)以語(yǔ)言環(huán)境有關(guān)的方式來(lái)格式化和解析日期的具體類

  • 進(jìn)行格式化(日期 --> 文本)、解析(文本 --> 日期)

  • 常用的時(shí)間模式字母

字母日期或時(shí)間示例
y2019
M年中月份08
d月中天數(shù)10
H1天中小時(shí)數(shù)(0-23)22
m分鐘16
s59
S毫秒367
public static void main(String [] args)throws Exception{
        // 創(chuàng)建SimpleDateFormat對(duì)象 y 年 M月
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
        //創(chuàng)建Date對(duì)象
        Date date = new Date();
        //格式化Date(把日期轉(zhuǎn)換成字符串)
        String str = sdf.format(date);
        System.out.println(str);

        //解析(把字符串轉(zhuǎn)換成日期)
        Date date2 = sdf.parse("1999/02/24");
        System.out.println(date2);

    }

System類

  • System系統(tǒng)類,主要用于獲取系統(tǒng)的屬性數(shù)據(jù)和其他操作,構(gòu)造方法私有的

方法名說(shuō)明
static void arraycopy(...)復(fù)制數(shù)組
static long currentTimeMillis();獲取當(dāng)前系統(tǒng)時(shí)間,返回的是毫秒值
static void gc();建議JVM趕快啟動(dòng)垃圾回收器回收垃圾
static void exit(int status);退出jvm,如果參數(shù)是0表示正常退出jvm,非0表示異常退出jvm

“String類的常用方法有哪些”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI