Java - String substring() 方法



Java String substring() 方法用於檢索 String 物件的子字串。子字串從給定索引處的字元開始,一直持續到當前字串的末尾。

由於 Java 中的 String 是不可變的,因此此方法始終返回一個新字串,而不會更改之前的字串。

此方法有兩個多型變體,其語法如下所示;一個只允許您指定起始索引,另一個允許您同時指定起始索引和結束索引。

語法

以下是Java String substring()方法的語法

public String substring(int beginIndex) // first syntax
or,
public String substring(int beginIndex, int endIndex) // second syntax

注意 - 如果未指定 endIndex,則該方法將返回從 startIndex 開始的所有字元。

引數

  • beginIndex - 這是起始索引的值,包含在內。// 第一種語法

  • endIndex - 這是結束索引的值,不包含在內。// 第二種語法

返回值

此方法返回指定的子字串。

示例:使用起始索引從字串中獲取子字串

以下示例演示了透過查詢字串“This is tutorials point”的子字串來使用 Java String substring() 方法。這裡,只將起始索引作為引數傳遞給此方法:

package com.tutorialspoint;

public class StringDemo {
   public static void main(String[] args) {
      String str = "This is tutorials point";
      String substr = "";    
      
      // prints the substring after index 7
      substr = str.substring(7);
      System.out.println("substring = " + substr); 
      
      // prints the substring after index 0 i.e whole string gets printed
      substr = str.substring(0);
      System.out.println("substring = " + substr);
   }
}

輸出

如果編譯並執行上述程式,它將產生以下結果:

substring = tutorials point
substring = This is tutorials point

示例:使用起始和結束索引從字串中獲取子字串

以下是一個示例,演示了透過查詢提供的字串的子字串在 Java 中使用 substring() 方法。這裡,字串的起始和結束位置都作為引數傳遞:

package com.tutorialspoint;

public class StringDemo {
   public static void main(String[] args) {
      String str = "This is tutorials point";
      String substr = "";    
      
      // prints the substring after index 7 till index 17
      substr = str.substring(7, 17);
      System.out.println("substring = " + substr); 
      
      // prints the substring after index 0 till index 7
      substr = str.substring(0, 7);
      System.out.println("substring = " + substr);
   }
}

輸出

讓我們編譯並執行上面的程式,輸出將顯示如下:

substring =  tutorials
substring = This is

示例:從括號之間的字串中獲取子字串

在這個示例中,我們嘗試從字串中檢索括號內的子字串。在這裡,我們使用 indexOf() 方法查詢左括號和右括號的索引。一旦我們獲得這些索引,我們就使用 substring() 方法來獲取此範圍內的子字串。

package com.tutorialspoint;

public class StringDemo {
   public static void main(String[] args) {
      String s = "Stabbed in the (back)";
      
      // getting substring between '(' and ')' delimeter
      int begin = s.indexOf("(");
      int ter = s.indexOf(")");
      String res = s.substring(begin + 1, ter);
      System.out.println("The substring is: " + res);
   }
}

輸出

執行上述程式後,輸出如下:

The substring is: back

示例:從括號之間的字串中獲取子字串

到目前為止,我們已經使用索引值來確定所需子字串的邊界。我們還可以列印兩個給定字串值之間存在的子字串。以下是一個示例:

package com.tutorialspoint;

public class StringDemo {
   public static void main(String[] args) {
      String s = "Stabbed in the (back)";
      
      // getting substring between '(' and ')' delimeter
      int begin = s.indexOf("(") + 1;
      int ter = s.indexOf(")");
      String res = s.substring(begin, ter);
      System.out.println("The substring is: " + res);
   }
}

輸出

以下是上述程式的輸出:

The substring is: back

示例:從字串中刪除第一個和最後一個字元

此程式中使用 substring() 方法從字串中刪除第一個和最後一個字元。

package com.tutorialspoint;

public class StringDemo {
   public static void main(String args[]) {
      String s = new String("Programming");
      System.out.print("Substring after eliminating the first character is: ");
      
      // excluding the first character since index(0) is excluded
      System.out.println(s.substring(1));
      System.out.print("Substring after eliminating the last character is: ");
      
      // excluding the last character by providing index as -1
      System.out.println(s.substring(0, s.length() - 1));
      System.out.println(
         "Substring after eliminating both the first and the last character is: " + s.substring(1, s.length() - 1));
   }
}

輸出

以下是上述程式碼的輸出:

Substring after eliminating the first character is: rogramming
Substring after eliminating the last character is: Programmin
Substring after eliminating both the first and the last character is: rogrammin
java_lang_string.htm
廣告
© . All rights reserved.