簡體   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