欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JAVA?String類中的一些常用方法示例詳解

 更新時間:2023年10月08日 10:58:29   作者:休息一下…  
在我們的工作中,常常要對一個字符串進行一些操作,這里提供一些常用的方法,常常需要這些方法進行組合處理字符串,這篇文章主要給大家介紹了關(guān)于JAVA?String類中的一些常用方法,需要的朋友可以參考下

字符串比較方法:

boolean equals(Object anObject):

比較兩個字符串是否相等,相等返回ture,否則返回false

    public static void main(String[] args) {
        String a = "asdf";
        System.out.println(a.equals("aaa"));
        System.out.println(a.equals("asdf"));
    }

int compareTo(String s):

比較兩個字符串是否相等,先按照字典次序大小比較,如果出現(xiàn)不等的字符,直接返回這兩個字符的大小差值;如果前k個字符相等(k為兩個字符長度最小值),返回兩個字符串長度差值。

    public static void main(String[] args) {
        String a = "asdf";
        System.out.println(a.compareTo("aaa"));
        System.out.println(a.compareTo("asdf"));
        System.out.println(a.compareTo("asd"));
    }

int compareToIgnoreCase(String str)

忽略字符大小寫進行比較,返回值規(guī)則為:

  • 先按照字典次序大小比較,如果出現(xiàn)不等的字符,直接返回這兩個字符的大小差值;
  • 如果前k個字符相等(k為兩個字符長度最小值),返回兩個字符串長度差值。
    public static void main(String[] args) {
        String a = "asdf";
        System.out.println(a.compareToIgnoreCase("aaa"));
        System.out.println(a.compareToIgnoreCase("ASDF"));
        System.out.println(a.compareToIgnoreCase("asd"));
    }

字符串查找方法:

char charAt(int index):

返回index位置上字符,如果index為負數(shù)或者越界,拋出IndexOutOfBoundsException異常。

    public static void main(String[] args) {
        String a = "asdf";
        System.out.println(a.charAt(0));
        System.out.println(a.charAt(3));
        System.out.println(a.charAt(5));
    }

int indexOf(int ch):

返回ch第一次出現(xiàn)的位置,沒有則返回-1

    public static void main(String[] args) {
        String a = "asdddf";
        System.out.println(a.indexOf('d'));
        System.out.println(a.indexOf('a'));
        System.out.println(a.indexOf('h'));
    }

int indexOf(int ch, int fromIndex):

從fromIndex位置開始找 ch 返回第一次出現(xiàn)的位置,沒有返回-1

    public static void main(String[] args) {
        String a = "asdddf";
        System.out.println(a.indexOf('d', 3));
        System.out.println(a.indexOf('a', 1));
        System.out.println(a.indexOf('h',0));
    }

int indexOf(String str):

返回str第一次出現(xiàn)的位置,沒有返回-1

    public static void main(String[] args) {
        String a = "asdddf";
        System.out.println(a.indexOf("dd"));
        System.out.println(a.indexOf("ss"));
    }

int indexOf(String str, int fromIndex):

從fromIndex位置開始找str第一次出現(xiàn)的位置,沒有返回-1

    public static void main(String[] args) {
        String a = "asdddf";
        System.out.println(a.indexOf("dd", 3));
        System.out.println(a.indexOf("ss", 0));
    }

int lastIndexOf(int ch):

后往前找,返回ch第一次出現(xiàn)的位置,沒有返回-1

    public static void main(String[] args) {
        String a = "asdddf";
        System.out.println(a.lastIndexOf('d'));
        System.out.println(a.lastIndexOf('s'));
        System.out.println(a.lastIndexOf('v'));
    }

int lastIndexOf(int ch, int fromIndex):

從fromIndex位置開始找,從后往前找ch第一次出現(xiàn)的位置,沒有返回-1

    public static void main(String[] args) {
        String a = "asdddf";
        System.out.println(a.lastIndexOf('d', 2));
        System.out.println(a.lastIndexOf('d', 3));
        System.out.println(a.lastIndexOf('d', 4));
        System.out.println(a.lastIndexOf('g', 5));
    }

int lastIndexOf(String str):

從后往前找,返回str第一次出現(xiàn)的位置,沒有返回-1

    public static void main(String[] args) {
        String a = "asdddf";
        System.out.println(a.lastIndexOf("dd"));
        System.out.println(a.lastIndexOf("as"));
        System.out.println(a.lastIndexOf("bv"));
    }

int lastIndexOf(String str, int fromIndex):

從后往前找str第一次出現(xiàn)的位置,如果此位置的下標不大于fromIndex則返回,否則繼續(xù)往前找。沒有返回-1

    public static void main(String[] args) {
        String a = "asdddf";
        System.out.println(a.lastIndexOf("dd", 3));
        System.out.println(a.lastIndexOf("dd", 2));
        System.out.println(a.lastIndexOf("dd", 1));
        System.out.println(a.lastIndexOf("as", 0));
        System.out.println(a.lastIndexOf("bv", 0));
    }

字符串的轉(zhuǎn)化

數(shù)字轉(zhuǎn)字符串

字符串轉(zhuǎn)整形

    public static void main(String[] args) {
        String str = "123";
        int a1 = Integer.parseInt(str);
        long a2 = Long.parseLong(str);
        System.out.println(a1+" "+a2);
    }

字符串轉(zhuǎn)浮點型: 

    public static void main(String[] args) {
        String str = "123";
        double a2 = Double.parseDouble(str);
        float a3 = Float.parseFloat(str);
        System.out.println(a2+" "+a3);
    }

String.valueOf():

所有基本類型值轉(zhuǎn)化為字符串類型

    public static void main(String[] args) {
        String s1 = String.valueOf(1234);
        String s2 = String.valueOf(12.34);
        String s3 = String.valueOf(true);
        String s4 = String.valueOf('a');
        System.out.println(s1);
        System.out.println(s2);
        System.out.println(s3);
        System.out.println(s4);
    }

String toUpperCase();

String toLowerCase():

返回一個將原字符串轉(zhuǎn)為大寫的新串 。

返回一個將原字符串轉(zhuǎn)為小寫的新串 。

    public static void main(String[] args) {
        String s1 = "heLLo";
        String s2 = "HEllO";
        System.out.println(s1.toUpperCase());
        System.out.println(s2.toLowerCase());
    }

char[] toCharArray();

String(char value[]):

將字符串轉(zhuǎn)為數(shù)組;原字符串不會受到影響

將數(shù)組轉(zhuǎn)為字符串;原數(shù)組不會受到影響

    public static void main(String[] args) {
        String s = "hello";
        char[] ch = s.toCharArray();
        System.out.println(Arrays.toString(ch));
        String s2 = new String(ch);
        System.out.println(s2);
    }

字符串替換方法:

String replaceAll(String regex, String replacement):

替換所有的指定內(nèi)容

    public static void main(String[] args) {
        String str = "helloworld" ;
        System.out.println(str.replaceAll("l", "O"));
    }

String replaceFirst(String regex, String replacement)

替換首個內(nèi)容

    public static void main(String[] args) {
        String str = "helloworld" ;
        System.out.println(str.replaceFirst("l", "O"));
    }

String[] split(String regex):

將字符串全部拆分

    public static void main(String[] args) {
        String str = "hello world hello" ;
        String[] result = str.split(" ") ; // 按照空格拆分
        for(String s: result) {
            System.out.println(s);
        }
    }

String substring(int beginIndex, int endIndex):

截取 [ beginIndex ,endIndex ) 范圍內(nèi)的字符串

    public static void main(String[] args) {
        String str = "helloworld" ;
        System.out.println(str.substring(0, 5));
    }

總結(jié)

到此這篇關(guān)于JAVA String類中的一些常用方法的文章就介紹到這了,更多相關(guān)JAVA String常用方法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • java短網(wǎng)址服務(TinyURL)生成算法

    java短網(wǎng)址服務(TinyURL)生成算法

    這篇文章主要為大家詳細介紹了java短網(wǎng)址服務生成算法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-08-08
  • Java繼承方法重寫實現(xiàn)原理及解析

    Java繼承方法重寫實現(xiàn)原理及解析

    這篇文章主要介紹了Java繼承方法重寫實現(xiàn)原理及解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-12-12
  • 詳解Java中Stream流的用法和原理

    詳解Java中Stream流的用法和原理

    最近編碼的時候用到了Stream這個東西,以前也用過,但是對它沒有一個系統(tǒng)的認知,在好奇心的驅(qū)動下還是決定花一些時間去系統(tǒng)地學一學,不了解Stream的同學可以看看本文,對大家的學習和工作有一定的幫助
    2023-10-10
  • Java中File類方法詳解以及實踐

    Java中File類方法詳解以及實踐

    Java File類的功能非常強大,利用java基本上可以對文件進行所有操作,下面這篇文章主要給大家介紹了關(guān)于Java中File類方法以及實踐的相關(guān)資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下
    2022-04-04
  • Java兩大工具庫Commons和Guava使用示例詳解

    Java兩大工具庫Commons和Guava使用示例詳解

    這篇文章主要為大家介紹了Java兩大工具庫Commons和Guava使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-02-02
  • java8 stream中Collectors.toMap空指針問題及解決

    java8 stream中Collectors.toMap空指針問題及解決

    這篇文章主要介紹了java8 stream中Collectors.toMap空指針問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-05-05
  • Jmeter如何基于命令行運行jmx腳本

    Jmeter如何基于命令行運行jmx腳本

    這篇文章主要介紹了Jmeter如何基于命令行運行jmx腳本,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-07-07
  • Windows中使用Java生成Excel文件并插入圖片的方法

    Windows中使用Java生成Excel文件并插入圖片的方法

    這篇文章主要介紹了Windows中使用Java生成Excel文件并插入圖片的方法,其中向Excel中插入圖片文中通過使用Apache POI來實現(xiàn),需要的朋友可以參考下
    2016-02-02
  • Java實現(xiàn)的質(zhì)因數(shù)分解操作示例【基于遞歸算法】

    Java實現(xiàn)的質(zhì)因數(shù)分解操作示例【基于遞歸算法】

    這篇文章主要介紹了Java實現(xiàn)的質(zhì)因數(shù)分解操作,結(jié)合實例形式較為詳細的分析了Java基于遞歸算法實現(xiàn)針對整數(shù)的質(zhì)因數(shù)分解相關(guān)操作技巧,需要的朋友可以參考下
    2018-03-03
  • Kafka源碼系列教程之刪除topic

    Kafka源碼系列教程之刪除topic

    這篇文章主要給大家介紹了關(guān)于Kafka源碼系列教程之刪除topic的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2018-08-08

最新評論