日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java-String的常用方法总结!

發布時間:2024/9/30 java 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java-String的常用方法总结! 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、String類

String類在java.lang包中,java使用String類創建一個字符串變量,字符串變量屬于對象。java把String類聲明的final類,不能繼承。String類對象創建后不能修改,由0或多個字符組成,包含在一對雙引號之間。

二、String類構造方法

1、public String()

無參構造方法,用來創建空字符串的String對象。

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類常用方法

1、public char charAt(int index)

參數

index – 字符的索引。

返回值

返回指定索引處的字符。

實例

public class Test {

public static void main(String args[]) {

String s = "www ";

char result = s.charAt(1);

System.out.println(result);

}

}

以上程序執行結果為:

w

2、public boolean equals(Object anObject)

參數

anObject – 與字符串進行比較的對象。

返回值

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

實例

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 );

}

}

以上程序執行結果為:

返回值 = true

返回值 = true

3、public boolean endsWith(String suffix)

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

參數

suffix – 指定的后綴。

返回值

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

實例

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 );

}

}

以上程序執行結果為:

返回值 = false

返回值 = true

4、public boolean equalsIgnoreCase(String anotherString)

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

參數

anObject – 與字符串進行比較的對象。

返回值

如果給定對象與字符串相等,則返回 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 );

}

}

以上程序執行結果為:

返回值 = true

返回值 = false

返回值 = true

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

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

參數

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’));

}

}

以上程序執行結果為:

返回值 :hellT

返回值 :heDDo

6、public String toLowerCase()

toLowerCase() 方法將字符串轉換為小寫。

參數

返回值

轉換為小寫的字符串。

public class Test {

public static void main(String args[]) {

String Str = new String(“WWW”);

System.out.print(“返回值 :” );

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

}

}

以上程序執行結果為:

返回值 :www

本文來自千鋒教育,轉載請注明出處。

總結

以上是生活随笔為你收集整理的Java-String的常用方法总结!的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。