LoginSignup
0
0

[Java]ある文字列が文字列のリストに含まれているか判定する

Last updated at Posted at 2023-06-06

NGワードのリストを作ってそこに含まれてるか調査する

こんな感じで判定できる。
前方一致ならcontainsをstartWithにする。

単純だけどこれが基本なんじゃないかな。forをforEach(拡張For)にするとngexitフラグがラムダの外になってビルドできない。

import java.util.List;

public class Main {
    public static void main(String[] args) {
        String mes = "ngaaa";

        var nglist = List.of("ng", "word", "list");

        boolean res = nglist.stream().anyMatch(ng -> mes.contains(ng));

        System.out.println(res);
    }
}

C#だとこんな感じ。

using System.Collections.Generic;

public class Hello{
    public static void Main(){
        string mes = "ngaaa";
        
        var nglist = new List<string>(){"ng","word","list"};
        
        var res = nglist.Exists(ng => mes.Contains(ng));
        
        System.Console.WriteLine(res);
    }
}
0
0
2

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