Java String contains 示例

时间:2020-02-23 14:35:12  来源:igfitidea点击:

String的 contains 方法检查是否可以在字符串中找到字符序列。
如果字符序列存在于eyly返回false中,则返回true返回false。

方法签名:

public boolean contains(CharSequence s)

字符串 contains 示例:

package org.igi.theitroad;
 
public class StringContainsMethodExample {
 
 public static void main(String[] args) {
  String str = "Hello world from theitroad.com";
 
  if (str.contains("world")) {
   System.out.println("String contains with world");
  } 
  else {
   System.out.println("String does not contain world");
  }
 
  if (str.contains("WORLD")) {
   System.out.println("String contains WORLD");
  } 
  else {
   System.out.println("String does not contain WORLD");
  }
 }
}

运行上面的程序时,我们将得到以下输出:

String contains with world
String does not contain WORLD

请注意,contains方法是区分大小写的,如上面的示例所示