Java 方法(转载)

时间:2025-02-15 09:32:18

描述

() 方法返回true,当且仅当此字符串包含指定的char值序列

声明

以下是声明()方法

public boolean contains(CharSequence s)

参数

  • s -- This is the sequence to search for.

返回值

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

异常

  • NullPointerException -- if s is null.

实例

下面的例子说明了如何使用()方法

package ;

import .*;

public class StringDemo {

  public static void main(String[] args) {
  
    String str1 = "tutorials point", str2 = "http://";

    CharSequence cs1 = "int";
    
    // string contains the specified sequence of char values
    boolean retval = (cs1);
    ("Method returns : " + retval);
    
    // string does not contain the specified sequence of char value
    retval = ("_");   
    ("Methods returns: " + retval);
  }
}

让我们来编译和运行上面的程序,这将产生以下结果:

Method returns : true
Methods returns: false