Java-String的常用方法有哪些-創(chuàng)新互聯(lián)

這篇文章主要介紹“Java-String的常用方法有哪些”,在日常操作中,相信很多人在Java-String的常用方法有哪些問(wèn)題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”Java-String的常用方法有哪些”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!

專(zhuān)注于為中小企業(yè)提供成都網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì)服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)蘆溪免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了上千家企業(yè)的穩(wěn)健成長(zhǎng),幫助中小企業(yè)通過(guò)網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。

一、String類(lèi)

String類(lèi)在java.lang包中,java使用String類(lèi)創(chuàng)建一個(gè)字符串變量,字符串變量屬于對(duì)象。java把String類(lèi)聲明的final類(lèi),不能繼承。String類(lèi)對(duì)象創(chuàng)建后不能修改,由0或多個(gè)字符組成,包含在一對(duì)雙引號(hào)之間。

二、String類(lèi)構(gòu)造方法

1、public String()

無(wú)參構(gòu)造方法,用來(lái)創(chuàng)建空字符串的String對(duì)象。

String str1 = new String();

String str2 = new String("asdf");

2、public String(String value)

String str2 = new String("asdf");

3、public String(char[] value)

char[] value = {'a','b','c','d'};

String str4 = new String(value);

4、public String(char chars[], int startIndex, int numChars)

char[] value = {'a','b','c','d'};

String str5 = new String(value, 1, 2);

5、public String(byte[] values)

byte[] strb = new byte[]{65,66};

String str6 = new String(strb);

三、String類(lèi)常用方法

1、public char charAt(int index)

參數(shù)

index -- 字符的索引。

返回值

返回指定索引處的字符。

實(shí)例

public class Test {

    public static void main(String args[]) {

        String s = "www ";

        char result = s.charAt(1);

        System.out.println(result);

    }

}

以上程序執(zhí)行結(jié)果為:

w

2、public boolean equals(Object anObject)

參數(shù)

anObject -- 與字符串進(jìn)行比較的對(duì)象。

返回值

如果給定對(duì)象與字符串相等,則返回 true;否則返回 false。

實(shí)例

public class Test {

    public static void main(String args[]) {

        String Str1 = new String("run");

        String Str2 = Str1;

        String Str3 = new String("run");

        boolean retVal;

        retVal = Str1.equals( Str2 );

        System.out.println("返回值 = " + retVal );

        retVal = Str1.equals( Str3 );

        System.out.println("返回值 = " + retVal );

    }

}

以上程序執(zhí)行結(jié)果為:

返回值 = true

返回值 = true

3、public boolean endsWith(String suffix)

endsWith() 方法用于測(cè)試字符串是否以指定的后綴結(jié)束。

參數(shù)

suffix -- 指定的后綴。

返回值

如果參數(shù)表示的字符序列是此對(duì)象表示的字符序列的后綴,則返回 true;否則返回 false。注意,如果參數(shù)是空字符串,或者等于此 String 對(duì)象(用 equals(Object) 方法確定),則結(jié)果為 true。

實(shí)例

public class Test {

    public static void main(String args[]) {

        String Str = new String("runooo");

        boolean retVal;

        retVal = Str.endsWith( "run" );

        System.out.println("返回值 = " + retVal );

        retVal = Str.endsWith( "ooo" );

        System.out.println("返回值 = " + retVal );

    }

}

以上程序執(zhí)行結(jié)果為:

返回值 = false

返回值 = true

4、public boolean equalsIgnoreCase(String anotherString)

equalsIgnoreCase() 方法用于將字符串與指定的對(duì)象比較,不考慮大小寫(xiě)。

參數(shù)

anObject -- 與字符串進(jìn)行比較的對(duì)象。

返回值

如果給定對(duì)象與字符串相等,則返回 true;否則返回 false。

public class Test {

    public static void main(String args[]) {

        String Str1 = new String("run");

        String Str2 = Str1;

        String Str3 = new String("run");

        String Str4 = new String("RUN");

        boolean retVal;

        retVal = Str1.equals( Str2 );

        System.out.println("返回值 = " + retVal );

        retVal = Str3.equals( Str4);

        System.out.println("返回值 = " + retVal );

        retVal = Str1.equalsIgnoreCase( Str4 );

        System.out.println("返回值 = " + retVal );

    }

}

以上程序執(zhí)行結(jié)果為:

返回值 = true

返回值 = false

返回值 = true

5、public String replace(char oldChar,char newChar)

replace() 方法通過(guò)用 newChar 字符替換字符串中出現(xiàn)的所有 oldChar 字符,并返回替換后的新字符串。

參數(shù)

oldChar -- 原字符。

newChar -- 新字符。

返回值

替換后生成的新字符串。

public class Test {

    public static void main(String args[]) {

        String Str = new String("hello");

        System.out.print("返回值 :" );

        System.out.println(Str.replace('o', 'T'));

        System.out.print("返回值 :" );

        System.out.println(Str.replace('l', 'D'));

    }

}

以上程序執(zhí)行結(jié)果為:

返回值 :hellT

返回值 :heDDo

6、public String toLowerCase()

toLowerCase() 方法將字符串轉(zhuǎn)換為小寫(xiě)。

參數(shù)

無(wú)

返回值

轉(zhuǎn)換為小寫(xiě)的字符串。

public class Test {

    public static void main(String args[]) {

         String Str = new String("WWW");

        System.out.print("返回值 :" );

        System.out.println( Str.toLowerCase() );

    }

}

以上程序執(zhí)行結(jié)果為:

返回值 :www

到此,關(guān)于“Java-String的常用方法有哪些”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注創(chuàng)新互聯(lián)-成都網(wǎng)站建設(shè)公司網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!

分享標(biāo)題:Java-String的常用方法有哪些-創(chuàng)新互聯(lián)
URL網(wǎng)址:http://www.muchs.cn/article26/shocg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供ChatGPT、外貿(mào)網(wǎng)站建設(shè)網(wǎng)站內(nèi)鏈用戶(hù)體驗(yàn)、電子商務(wù)、品牌網(wǎng)站制作

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站建設(shè)