예제 #1
0
        public static string[] SpecialSplit(string s, char c)
        {
            var resultList = new List <string>();
            int i          = 0;

            while (i < s.Length)
            {
                if (Bracket.IsOpen(s[i]))
                {
                    i = MatchBracket(s, i);
                }
                if (i > 0)
                {
                    if (c == s[i] && (!Ops.Contains(s[i - 1])))
                    {
                        resultList.Add(s.Substring(0, i));
                        s = s.Substring(i + 1);
                        i = -1;
                    }
                }
                i++;
            }
            resultList.Add(s);
            string[] result = resultList.ToArray();
            return(result);
        }
예제 #2
0
 public static string RemoveOuterBracket(string s)
 {
     if (Bracket.IsOpen(s[0]))
     {
         if (MatchBracket(s, 0) == s.Length - 1)
         {
             return(RemoveOuterBracket(s.Substring(1, s.Length - 2)));
         }
     }
     return(s);
 }
예제 #3
0
        public static int MatchBracket(string s, int open)
        {
            string queue = s[open].ToString(CultureInfo.InvariantCulture);

            if (Bracket.IsOpen(s[open]))
            {
                for (int i = open + 1; i < s.Length; i++)
                {
                    if (Bracket.IsOpen(s[i]))
                    {
                        queue = s[i] + queue;
                    }
                    else if (s[i] == Bracket.Match(queue[0]))
                    {
                        queue = queue.Substring(1);
                    }
                    if (queue.Length == 0)
                    {
                        return(i);
                    }
                }
            }
            else if (Bracket.IsClose(s[open]))
            {
                for (int i = open - 1; i >= 0; i--)
                {
                    if (Bracket.IsClose(s[i]))
                    {
                        queue = s[i] + queue;
                    }
                    else if (s[i] == Bracket.Match(queue[0]))
                    {
                        queue = queue.Substring(1);
                    }
                    if (queue.Length == 0)
                    {
                        return(i);
                    }
                }
            }
            else
            {
                return(-1);
            }
            throw new Exception("!! Brackets not matched");
        }