繁体   English   中英

如何检查字符串是否包含数字和字母

[英]How to check if a string has numbers and letters of alphabet

我想知道字符串是否具有可以查看字符串是否通常包含单词或单词和其他类型字符的功能。 (理想情况下,我只想考虑具有可读句子但可能过于高级的字符串)我正在尝试计算列表中具有此类文本的字符串。 我不想将任何仅包含数字、空格、制表符或符号(如?、@、#)甚至随机字母的字符串添加到计数中,除非字符串中至少包含一些单词。

字符串将使用 "" 进行初始化,因此 if 语句中的代码 - (this.ElementAt(i).getNotarised().ElementAt(j).getError() != "") - 如果没有输入就没问题根本。 不幸的是,我在某些情况下期待一些输入,所以这种情况也会计算只有空格、数字、符号或全部 3 个的场景——我不想要这些。

这是指从 List.getNotarised() 继承的 class 返回一个列表

/** Find the highest/lowest amount of errors/solutions/suggestions/comments.
         *  If it's ASC then return the lowest to highest.
         *  If it's DESC then return the highest to lowest.
         *  Return a list with LOGS in the correct order.
         *  */
        public List<LOG> highORlow(string asc_desc, string category)
        {
            int count = 0;
            int[] array = new int[this.Count()];

            for(int i = 0; i <= this.Count()-1; i++)
            {
                for(int j = 0; j <= this.ElementAt(i).getNotarised().Count()-1; j++)
                {
                    if(this.ElementAt(i).getNotarised().ElementAt(j).getError() != "")
                    {
                        ++count;
                    }
                }
                array[i] = count;

            }



            return new List<LOG>();
        }

兴趣点: string pattern = "[A-Za-z]{5}[0-9]{0}"; Regex xp = new Regex(模式);

使用xp.IsMatch()检查模式是否匹配。

/** Find the highest/lowest amount of errors/solutions/suggestions/comments.
         *  If it's true (ASC) then return the lowest to highest.
         *  If it's false (DESC) then return the highest to lowest.
         *  Return a list with LOGS in the correct order.
         *  */
        public List<LOG> highORlow(bool asc_desc, string category)
        {
            int count = 0;
            string pattern = "[A-Za-z]{5}[0-9]{0}";
            Regex xp = new Regex(pattern);
            Dictionary<LOG, int> array = new Dictionary<LOG, int>();


            for (int i = 0; i <= this.Count() - 1; i++)
            {
                for (int j = 0; j <= this.ElementAt(i).getNotarised().Count() - 1; j++)
                {
                    if (category == "ERROR")
                    {
                        if (xp.IsMatch(this.ElementAt(i).getNotarised().ElementAt(j).getError()))
                        {
                            ++count;
                            continue;
                        }
                    }
                    if (category == "SOLUTION")
                    {
                        if (xp.IsMatch(this.ElementAt(i).getNotarised().ElementAt(j).getSolution()))
                        {
                            ++count;
                            continue;
                        }
                    }
                    if (category == "SUGGESTION")
                    {
                        if (xp.IsMatch(this.ElementAt(i).getNotarised().ElementAt(j).getSuggestion()))
                        {
                            ++count;
                            continue;
                        }
                    }
                    if (category == "COMMENT")
                    {
                        if (xp.IsMatch(this.ElementAt(i).getNotarised().ElementAt(j).getComment()))
                        {
                            ++count;
                            continue;
                        }
                    }
                }
                array.Add(this.ElementAt(i), count);
                count = 0;
            }

            List<LOG> list = new List<LOG>();
            
            if(asc_desc)
            {
                foreach (KeyValuePair<LOG, int> v in array.OrderBy(key => key.Value))
                {
                    list.Add(v.Key);
                }
            }
            else
            {
                foreach (KeyValuePair<LOG, int> v in array.OrderByDescending(key => key.Value))
                {
                    list.Add(v.Key);
                }
            }



            return list;
        }```

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM