コード例 #1
0
        // Count the number of characters matching the option.
        public static int CountChars(string str, SeparateOptions option)
        {
            string found, notFound;

            Separate(str, out found, out notFound, option);
            return(found.Length);
        }
コード例 #2
0
        // Separate characters from a string into two strings.
        public static void Separate(string str, out string found, out string notFound, SeparateOptions option)
        {
            found    = "";
            notFound = "";

            foreach (char ch in str)
            {
                switch (option)
                {
                case SeparateOptions.Digits:
                    if (char.IsDigit(ch))
                    {
                        found += ch;
                    }
                    else
                    {
                        notFound += ch;
                    }
                    break;

                case SeparateOptions.Letters:
                    if (char.IsLower(ch) || char.IsUpper(ch))
                    {
                        found += ch;
                    }
                    else
                    {
                        notFound += ch;
                    }
                    break;

                case SeparateOptions.Punctuation:
                    if (char.IsPunctuation(ch))
                    {
                        found += ch;
                    }
                    else
                    {
                        notFound += ch;
                    }
                    break;

                case SeparateOptions.WhiteSpace:
                    if (char.IsWhiteSpace(ch))
                    {
                        found += ch;
                    }
                    else
                    {
                        notFound += ch;
                    }
                    break;
                }
            }
        }