Java - String startsWith() 方法



Java String startsWith() 方法用於檢查字串是否以指定的開頭字串開始。需要驗證的開頭字串以字串的形式提供給函式。單個字元也可以作為開頭字串。

如果字串包含指定的開頭字串,則此方法返回布林值 true;否則返回 false。例如,對於字串“TutorialsPoint”,如果透過傳遞“Tut”作為引數來呼叫此方法,則它將返回布林值 true

此方法有兩個多型變體,其語法如下所示。

語法

以下是Java String startsWith()方法的語法:

public boolean startsWith(String prefix) // first syntax
or,
public boolean startsWith(String prefix, int toffset) // second syntax

引數

  • prefix − 這是開頭字串的值。

  • toffset − 從字串的哪個位置開始查詢。// 第二種語法

返回值

  • public boolean startsWith(String prefix)方法如果引數表示的字元序列是此字串表示的字元序列的字首,則返回 true,否則返回 false。

  • public boolean startsWith(String prefix, int toffset)方法如果引數表示的字元序列是此物件從索引 toffset 開始的子字串的字首,則返回 true,否則返回 false。

檢查字串是否以給定字串開頭示例

以下示例顯示了 Java String startsWith() 方法的使用。在這裡,我們用 URL 初始化一個字串,並驗證它是否以“www”開頭:

package com.tutorialspoint;

public class StringDemo {
   public static void main(String[] args) {
      String str = "www.tutorialspoint.com";
      System.out.println(str); 
      
      // the start string to be checked
      String startstr1 = "www";
      
      // checks that string starts with given substring
      boolean retval1 = str.startsWith(startstr1);
      
      // prints true if the string starts with given substring
      System.out.println("starts with " + startstr1 + " ? " + retval1);
   }
}

輸出

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

www.tutorialspoint.com
starts with www ? true

檢查字串是否不以給定字串開頭示例

以下是此函式的另一個示例:

package com.tutorialspoint;

public class StringDemo {
   public static void main(String[] args) {
      String str = "www.tutorialspoint.com";
      System.out.println(str);
      
      // the start string to be checked
      String startstr1 = "com";
      
      // checks that string starts with given substring
      boolean retval1 = str.startsWith(startstr1);
      
      // prints true if the string starts with given substring otherwise false
      System.out.println("starts with " + startstr1 + " ? " + retval1);
   }
}

輸出

如果編譯並執行上面的程式,則輸出將顯示如下:

www.tutorialspoint.com
starts with com ? false

檢查字串是否以給定字串開頭示例

以下示例顯示了 Java String startsWith() 方法的使用,方法是初始化一個 String 物件並透過提供索引來啟動匹配。這裡它返回失敗的匹配:

package com.tutorialspoint;

public class StringDemo {
   public static void main(String[] args) {
      String str = "www.tutorialspoint.com";
      System.out.println(str); 
      
      // the start string to be checked
      String startstr1 = "tutorialspoint";
      
      // checks that string starts with given substring and starting index
      boolean retval1 = str.startsWith(startstr1);
      
      // prints true if the string starts with given substring
      System.out.println("starts with " + startstr1 + " ? " + retval1);
   }
}

輸出

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

www.tutorialspoint.com
starts with tutorialspoint ? false

從偏移量檢查字串是否以給定字串開頭示例

一個 String 物件被初始化為“www.tutorialspoint.com”,並且我們透過傳遞 toffset 值以及開頭字串來在此字串上呼叫 startsWith() 方法:

package com.tutorialspoint;

public class StringDemo {
   public static void main(String[] args) {
      String str = "www.tutorialspoint.com";
      System.out.println(str);
      
      // the start string to be checked
      String startstr1 = "tutorialspoint";
      
      // checks that string starts with given substring and starting index
      boolean retval1 = str.startsWith(startstr1, 4);
      // prints true if the string starts with given substring otherwise false
      System.out.println("string " + startstr1 + " starting from index 4 ? " + retval1);
   }
}

輸出

上述程式的輸出如下:

www.tutorialspoint.com
string tutorialspoint starting from index 4 ? true

在檢查字串是否以給定字串開頭時出現異常示例

如果我們傳遞 null 作為引數,則會丟擲 NullPointerException。原因是 null 不能用作引數。

public class StringDemo {
   public static void main(String[] args) {
      String s = "Welcome to tutorials Point";
      System.out.println("The string is: " + s.startsWith(null));
   }
}

NullPointerException

執行上述程式碼時,我們得到以下輸出:

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "String.length()" because "prefix" is null
      at java.base/java.lang.String.startsWith(String.java:2253)
      at java.base/java.lang.String.startsWith(String.java:2296)
      at StringDemo.main(StringDemo.java:6)
java_lang_string.htm
廣告