SQL の In 句の様な事を VB/C# で
- 複数の値のどれかと一致するかどうかを調べたい ( C# と VB.NET の質問掲示板)
つまり、まとめるとこう云う事ですね?
VB
Imports System.Runtime.CompilerServices Module StringExtensions <Extension()> _ Public Function [In](ByVal str As String, ParamArray ByVal params As String()) As Boolean Return params.Contains(str) End Function End Module Module Foo Sub Main() Dim str1 As String = "peach" If str1.In("banana", "melon", "peach") Then Console.WriteLine("見つかったヨ!") Else Console.WriteLine("入ってないヨ!") End If End Sub End Module
C#
using System; using System.Linq; public static class StringExtensions { public static bool In(this string str, params string[] param) { return param.Contains(str); } } class Foo { static void Main() { var str = "peach"; if (str.In("banana", "melon", "peach")) { Console.WriteLine("見つかったヨ!"); } else { Console.WriteLine("入ってないヨ!"); } } }
……意外と使えるかもしれない。