最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

JAVA?String類(lèi)中的一些常用方法示例詳解

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

字符串比較方法:

boolean equals(Object anObject):

比較兩個(gè)字符串是否相等,相等返回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):

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

    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)

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

  • 先按照字典次序大小比較,如果出現(xiàn)不等的字符,直接返回這兩個(gè)字符的大小差值;
  • 如果前k個(gè)字符相等(k為兩個(gè)字符長(zhǎng)度最小值),返回兩個(gè)字符串長(zhǎng)度差值。
    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為負(fù)數(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)的位置,沒(méi)有則返回-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位置開(kāi)始找 ch 返回第一次出現(xiàn)的位置,沒(méi)有返回-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)的位置,沒(méi)有返回-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位置開(kāi)始找str第一次出現(xiàn)的位置,沒(méi)有返回-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)的位置,沒(méi)有返回-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位置開(kāi)始找,從后往前找ch第一次出現(xiàn)的位置,沒(méi)有返回-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)的位置,沒(méi)有返回-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)的位置,如果此位置的下標(biāo)不大于fromIndex則返回,否則繼續(xù)往前找。沒(méi)有返回-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)浮點(diǎ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():

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

    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():

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

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

    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ù)組;原字符串不會(huì)受到影響

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

    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)

替換首個(gè)內(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類(lèi)中的一些常用方法的文章就介紹到這了,更多相關(guān)JAVA String常用方法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java選擇排序法以及實(shí)例詳解

    Java選擇排序法以及實(shí)例詳解

    在本篇文章里小編給大家整理了一篇關(guān)于Java選擇排序法以及實(shí)例內(nèi)容,并做了詳細(xì)分析,有興趣的朋友們可以跟著學(xué)習(xí)下。
    2022-11-11
  • IDEA初次拉取GitLab項(xiàng)目過(guò)程

    IDEA初次拉取GitLab項(xiàng)目過(guò)程

    本文介紹了如何在計(jì)算機(jī)上安裝Git工具,并如何使用GitLab獲取訪問(wèn)令牌,同時(shí),還提供了如何添加GitLab服務(wù)、第一次代碼準(zhǔn)備以及遇到的一些常見(jiàn)問(wèn)題及其解決方案
    2026-01-01
  • Java中import java.util.Scanner的用處詳解

    Java中import java.util.Scanner的用處詳解

    文章主要介紹Java中的Scanner類(lèi)及其常用方法next()和nextLine()的區(qū)別,next()方法在遇到空格、Tab鍵、回車(chē)鍵等分隔符時(shí)結(jié)束輸入,而nextLine()方法則接收所有輸入,直到遇到回車(chē)鍵
    2024-11-11
  • Java編程 多態(tài)

    Java編程 多態(tài)

    這篇文章主要介紹了關(guān)于Java編程的多態(tài),多態(tài)通過(guò)分離做什么和怎么做,從另一個(gè)角度將接口和實(shí)現(xiàn)分離開(kāi)來(lái)。構(gòu)建可擴(kuò)展的程序,需要的朋友可以參考下
    2021-10-10
  • mybatis Plus 多表聯(lián)合查詢(xún)的實(shí)現(xiàn)示例

    mybatis Plus 多表聯(lián)合查詢(xún)的實(shí)現(xiàn)示例

    這篇文章主要介紹了mybatis Plus 多表聯(lián)合查詢(xún)的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • java實(shí)現(xiàn)壓縮字符串和java字符串過(guò)濾

    java實(shí)現(xiàn)壓縮字符串和java字符串過(guò)濾

    這篇文章主要介紹了java實(shí)現(xiàn)壓縮字符串和java字符串過(guò)濾,需要的朋友可以參考下
    2014-04-04
  • OAuth2生成token代碼備忘實(shí)現(xiàn)過(guò)程示例

    OAuth2生成token代碼備忘實(shí)現(xiàn)過(guò)程示例

    這篇文章主要為大家介紹了OAuth2生成token代碼備忘實(shí)現(xiàn)過(guò)程示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-08-08
  • Spring啟動(dòng)時(shí)實(shí)現(xiàn)初始化的幾種方案

    Spring啟動(dòng)時(shí)實(shí)現(xiàn)初始化的幾種方案

    這篇文章主要介紹了Spring啟動(dòng)時(shí)實(shí)現(xiàn)初始化的幾種方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-06-06
  • 基于String不可變字符與StringBuilder可變字符的效率問(wèn)題

    基于String不可變字符與StringBuilder可變字符的效率問(wèn)題

    這篇文章主要介紹了String不可變字符與StringBuilder可變字符的效率問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • Java并發(fā)工具類(lèi)LongAdder原理實(shí)例解析

    Java并發(fā)工具類(lèi)LongAdder原理實(shí)例解析

    這篇文章主要介紹了Java并發(fā)工具類(lèi)LongAdder原理實(shí)例解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-05-05

最新評(píng)論

太白县| 霍州市| 新化县| 清流县| 陆河县| 衡水市| 安泽县| 缙云县| 梁山县| 无棣县| 上栗县| 西平县| 尉氏县| 确山县| 杨浦区| 杭锦旗| 安新县| 花莲县| 玛多县| 呼玛县| 涞水县| 宜君县| 文昌市| 微山县| 康马县| 河南省| 久治县| 安图县| 工布江达县| 新野县| 阿拉善左旗| 衡东县| 新河县| 张掖市| 宜阳县| 芦山县| 东丰县| 开江县| 凌海市| 内江市| 贡山|