0
0

More than 3 years have passed since last update.

Java containsメソッドと用途

Posted at

containsメソッドとは?

containsメソッドは特定の要素を含むかどうか判定するメソッドです。

サンプルコード

標準入力で入力された要素が要素の何に含まれているかみます

入力例

Z
Kirishima
Main.java
import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String a = sc.next();           //1行目の要素を取り出す
        String s = sc.next();           //2行目の要素をたりだす

        if (s.contains(a)) {            //変数sの要素の中にaの要素が含まれているかみます
            System.out.println("YES");
        } else {
            System.out.println("NO");
        }
    }
}

出力結果

NO
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0