コガネブログ

平日更新を目標に Unity や C#、Visual Studio、ReSharper などのゲーム開発アレコレを書いていきます

【C#】指定されたいずれかの文字列を含むかどうかを返す拡張メソッド

ソースコード

using System;
using System.Linq;

public static class StringExtensions
{
    public static bool IncludeAny( 
        this string self, 
        params string[] list 
    )
    {
        return list.Any( c => self.Contains( c ) );
    }
}

使い方

var str = "ピカチュウカイリュー";
Debug.Log( str.IncludeAny( "ピカチュウ", "カイリュー" ) ); // True
Debug.Log( str.IncludeAny( "カイリュー", "ヤドラン" ) ); // True
Debug.Log( str.IncludeAny( "ヤドラン", "ピジョン" ) ); // False

関連記事