Windows窗体文本编辑器中的粗体字计数

时间:2018-12-17 09:39:22

标签: c# html winforms rich-text-editor

我让用户在Windows窗体文本编辑器中输入字符串,文本存储为html,粗体字封装在<STRONG>abc </STRONG>标记中。 每个单词都会被计数和显示,但粗体单词会被计数两次。 如何计算粗体字的总数。 目前,我提取了单词并将其与原始字符串进行比较,但是这种方法不起作用。

//test is a keyvalue pair that contains count of each word
MatchCollection matches = Regex.Matches(source, @"<STRONG.*?>(.*?)<\/STRONG>");
            List<string> results=new List<String>();
            if (matches.Count > 0)
                foreach (Match m in matches)
                    results.Add(m.Groups[1].ToString());

            int boldword = 0;

            List<string> finalist = new List<String>();
            foreach (string aa in results)
            {
                string abc = aa;
                string[] words = abc.Split(' ');
                for (int w = 0; w < words.Length; w++)
                {
                    if (!IsValid(words[w]) && !IsUrlValid(words[w]) && !IsDecimal(words[w]))
                        words[w] = words[w].Replace('.', ' ');
                    words = words.Where(x => !string.IsNullOrEmpty(x)).ToArray();
                }
                    string nword = string.Join(" ", words);
                    nword = nword.TrimEnd();
                    string[] nwrdarr = nword.Split(' ');
                    finalist.AddRange(nwrdarr);
            }
            for (int ab = 0; ab < finalist.Count; ab++)
            {
                if (finalist[ab] != "")
                {
                    string n = test.ElementAt(ab).Key.ToString();
                    List<String> testn = new List<String>();
                    if (!IsValid(n) && !IsUrlValid(n) && !IsDecimal(n))
                    {
                        testn = n.Split('.').ToList();
                    }
                    if (testn.Count > 0)
                    {
                        var sbc = new StringBuilder();
                        foreach (char c in testn[0])
                        {
                            if (!char.IsPunctuation(c))
                                sbc.Append(c);
                        }
                        testn[0] = sbc.ToString();
                        var sb = new StringBuilder();
                        foreach (char c in finalist[ab])
                        {
                            if (!char.IsPunctuation(c))
                                sb.Append(c);
                        }
                        finalist[ab] = sb.ToString();
                        if (testn[0] == finalist[ab])
                        {
                            boldword = boldword + test[ab].Value;
                            int val = test[ab].Value * 2;
                            var newEntry = new KeyValuePair<string, int>(finalist[ab], val);
                            test[ab] = newEntry;
                        }
                    }
                    else
                    {
                        if (n == finalist[ab])
                        {
                            boldword = boldword + test[ab].Value;
                            int val = test[ab].Value * 2;
                            var newEntry = new KeyValuePair<string, int>(finalist[ab], val);
                            test[ab] = newEntry;
                        }
                    }
                }
            }

0 个答案:

没有答案
相关问题