5
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

文字列に指定した文字列が含まれているか?

Posted at

文字列内が複数のの文字列の何れかを含んでいるか

任意の文字列を含んでいるか?なら以下で事足りる。

string s = "abcdefg";
s.Contains("bcd");     // true
  

SQLのINのように書いてみたかったので、以下の様にしてみた。

    public static class GenericExtensions
    {
        public static bool Contains(this string str, params string[] param)
        {
            foreach (string s in param)
            {
                if (str.Contains(s))
                    return true;
            }
            return false;
        }
        public static bool In(this string str, params string[] param)
        {
            foreach (string s in param)
            {
                if (str.Contains(s))
                    return true;
            }
            return false;
        }
    }

    static void TestIn()
    {
        var inCause = new string[] { @"ABC", @"DEF", @"XYZ" };
        var testText1 = @"A B C DEF ZZZ";
        var testText2 = @"A B C D E F ZZZ";
        testText1.Contains(inCause);
        testText2.In("XYZ", "b");
    }

微妙な感じだけれども、用は足りたので、OKとしている。

5
3
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
5
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?