LoginSignup
8
9

More than 5 years have passed since last update.

c# カッコで始まってカッコで終わる文字列を取得

Last updated at Posted at 2015-05-11
Strings.cs
public static string GetEnclosedString(string str)
{
    var started = false;
    var openCount = 0;
    for (var count = 0; count < str.Length; count++)
    {
        switch (str[count])
        {
            case '(': openCount++; started = true; break;
            case ')': openCount--; break;
        }
        if (started && openCount == 0)
        {
            return str.Substring(0, count + 1);
        }
    }
    return str;
}

// 正規表現とかでシンプルに解決する方法ないかな。。。

8
9
3

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
8
9