Java-String的常用方法總結!

千鋒武漢發表於2021-04-25

   一、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


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/31548651/viewspace-2769709/,如需轉載,請註明出處,否則將追究法律責任。

相關文章