Java - String contains() 方法



描述

Java String contains() 方法用於檢查當前字串是否包含指定的序列。它返回布林值,當且僅當字串包含字元值的序列時返回 true;否則返回 false。

contains() 方法接受一個字元序列作為引數。如果序列之一包含空值,則會丟擲異常。字元序列是字元值的可讀序列。

語法

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

public boolean contains(CharSequence s)

引數

  • s - 這是要搜尋的序列。

返回值

如果此字串包含 s,則此方法返回 true,否則返回 false。

檢查字串是否包含 CharSequence 示例

如果給定的字串值包含指定的字元序列值,則 contains() 方法返回true

在下面的程式中,我們使用值“Java Programming”例項化字串類。然後,我們使用值“Java”建立字元序列。使用contains() 方法,我們嘗試檢查給定字串是否包含指定的字元序列。

package com.tutorialspoint;

public class Concat {
   public static void main(String[] args) {
      
      //instantiate the string class
      String str1 = new String("Java Programming");
      System.out.println("The given string: " + str1);
      
      //initialize the char sequence
      CharSequence str2 = "Java";
      System.out.println("The given char sequence is: " + str2);
      
      //using the contains() method
      boolean bool = str1.contains(str2);
      System.out.println("The string contains the specified char sequence or not? " + bool);
   }
}

輸出

執行上述程式後,將產生以下結果:

The given string: Java Programming
The given char sequence is: Java
The string contains the specified char sequence or not? true

檢查字串是否不包含 CharSequence 示例

如果給定的字串值不包含指定的字元序列值,則 contains() 方法返回false

在下面的程式中,我們使用值“HelloWorld”建立一個字串類物件。然後,我們使用值“helo”建立字元序列。使用contains() 方法,我們嘗試檢查給定字串是否包含指定的字元序列。

package com.tutorialspoint;

public class Concat {
   public static void main(String[] args) {
      
      // creating an object the string class
      String str1 = new String("HelloWorld");
      System.out.println("The given string: " + str1);
      
      //initialize the char sequence
      CharSequence str2 = "helo";
      System.out.println("The given char sequence is: " + str2);
      
      //using the contains() method
      boolean bool = str1.contains(str2);
      System.out.println("The string contains the specified char sequence or not? " + bool);
   }
}

輸出

以下是上述程式的輸出:

The given string: HelloWorld
The given char sequence is: helo
The string contains the specified char sequence or not? false

在檢查字串是否包含 CharSequence 時獲取異常的示例

如果給定的字元序列值為null,則此方法會丟擲NullPointerException

在此程式中,我們使用值“hello”建立一個字串字面量。然後,我們使用null 值建立一個字元序列。使用contains() 方法,我們嘗試檢查給定字串是否包含指定的字元序列 null。

public class Concat {
   public static void main(String[] args) {
      try {
         
         // instantiate the string class
         String str1 = "hello";
         System.out.println("The given string: " + str1);
         CharSequence str2 = null;
         System.out.println("The given char sequence is: " + str2);
         
         //using the contains() method
         boolean bool = str1.contains(str2);
         System.out.println("The string conatins the specified char seqience or not? " + bool);
      } catch(NullPointerException e) {
         e.printStackTrace();
         System.out.println("Exception: " + e);
      }
   }
} 

輸出

上述程式產生以下輸出:

The given string: hello
The given char sequence is: null
java.lang.NullPointerException: Cannot invoke "java.lang.CharSequence.toString()" because "s" is null
	at java.base/java.lang.String.contains(String.java:2854)
	at com.tutorialspoint.StringBuilder.Concat.main(Concat.java:11)
Exception: java.lang.NullPointerException: Cannot invoke "java.lang.CharSequence.toString()" because "s" is null

在區分大小寫模式下檢查字串是否包含 CharSequence 的示例

如果給定的字串和字元序列值相同,但大小寫不同,則 contains() 方法返回false

在此示例中,我們使用值“TUTORIX”例項化字串類。然後,我們使用值“tutorix”建立字元序列。使用contains() 方法,我們嘗試比較字串是否包含指定的字元序列。

package com.tutorialspoint;

public class Concat {
   public static void main(String[] args) {
      
      // instantiate the string class
      String str1 = new String("TUTORIX");
      System.out.println("The given string: " + str1);
      CharSequence str2 = "tutorix";
      System.out.println("The given char sequence is: " + str2);
      
      //using the contains() method
      boolean bool = str1.contains(str2);
      System.out.println("The contains() method return: " + bool);
      if(bool) {
         System.out.println("The given string contains the spcified char sequence");
      } else {
         System.out.println("The given string does not contain the specified char sequence");
      }
   }
}

輸出

執行上述程式後,將生成以下輸出:

The given string: TUTORIX
The given char sequence is: tutorix
The contains () method return: false
The given string does not contain the specified char sequence
java_lang_string.htm
廣告