Mycode中发生重复 [英] Duplication is occuring in mycode

查看:105
本文介绍了Mycode中发生重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我的代码有问题.任何人都可以帮助我...

问题:我有两个文本框,一个文本框应接受短语(单词),另一个文本框应接受数字(1-9)...
如果我在第二个文本框中输入了数字,则应根据文本框编号对gridview中的短语(单词)进行排序.
验证是第一个文本框,不应接受重复的名称或短语.
我已经写过这样的代码,但是它接受重复的名称并在gridview中显示...实际上,它不应接受重复的.....所以请任何人帮帮我.... pls .. ...预先感谢.....

我的代码是这样的:

Hi,

I have a problem in my code.pls can anyone help me...

Problem: I have two textboxes one textbox should accept phrases(words) and another one should accept number(1-9)...
if i have entered number in the second textbox.according to textbox number it should sort the phrases(words)in gridview..
validations are first textbox shouldnot accept the duplicate name or phrases.
I have written code like this but it is accepting duplicate names and displaying in gridview...actually it should not accept duplicate.....so please can any one help me out.........pls.....Thanks in Advance........

my code is like this:

{
        string phrses = TextBox1.Text;
        string[] split = phrses.Split(',');
        int count = phrses.Length - phrses.Replace(",", "").Length;
        int i = Convert.ToInt32(TextBox2.Text);

        int k1 = 0; int a1 = 0; int b = 0;
        for (a1 = count; a1 > 0; a1--)
        {
            for (b = count; b > 0; b--)
            {
                if (a1 != b)
                {
                    if (split[a1] == split[b])
                    {
                        k1 = 1;
                    }
                }
            }
        }
        if (k1 != 0)
        {
            Response.Write("enter different phrases");
        }
        string[] splichar = new string[split.Length];
        int j = 0;
        foreach (string a in split)
        {
            splichar[j] = a.Substring(i - 1);
            j++;
        }
        Array.Sort(splichar);

        j = 0;
        foreach (string k in splichar)
        {
            foreach (string p in split)
            {
                if (p.Contains(k))
                {
                    splichar[j] = p;
                }
            }
            j++;
        }


请帮我.........

添加了代码标签-LOSMAC [/EDIT]


pls help me.........

Code tags added - LOSMAC[/EDIT]

推荐答案

可以检查在给定的单词组separated by ,中是否重复了word or phrase使用LINQRegex 如下所示:

It can be checked whether a word or phrase is repeated in the given group of words separated by , either using LINQ or Regex as shown below:

string words1 = "abcd,defgh xyz, abcd, defgh, abcd,  abcd, ijkl";
string words2 = "abcd,defgh xyz,  ijkl, mnop, qrst, uvw, abc, uvws, defghxyz";
int max = words1.Split(',').Select (w => w.Trim())
                .GroupBy (w => w).Max (w => w.Count ());
Console.WriteLine (max);

bool result = Regex.IsMatch(words1,
                @"(?:^\s*(?<word>[^,]+)\s*,|,\s*(?<word>[^,]+)\s*,)"+
                @"(?=.*,\s*\k<word>\s*,|.*,\s*\k<word>\s*


)", RegexOptions.CultureInvariant | RegexOptions.IgnoreCase); Console.WriteLine(结果); // words1的输出 // 4 // 真实 // words2的输出 // 1 // 错误
)", RegexOptions.CultureInvariant | RegexOptions.IgnoreCase); Console.WriteLine (result); //Output for words1 //4 //True //Output for words2 //1 //False


可以在此处 http://regexhero.net/tester/ [


The regular expression can be tested here http://regexhero.net/tester/[^]


我不确定我是否完全理解代码.这行似乎很奇怪:

I am not sure I totally understand the code. This line seems strange:

int count = phrses.Length - phrses.Replace(",", ";").Length;



原因是替换"不应更改长度,因此计数应始终为零.您调试了吗?



The reason is that the Replace should not change the length, so the count should always be zero. Did you debug this?


这篇关于Mycode中发生重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆