且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在逗号分隔的文本框中重复值的问题

更新时间:2023-11-26 08:22:46

for (int i = 0; i < sl.Length; i++)
                        {
                            for (int j = i + 1; j < sl.Length; j++)
                            {
                                if (sl[i].ToString() == sl[j].ToString())
                                {
                                    args.IsValid = false;
//you should break the loop here
break;
                                }
                                else
                                {
                                    args.IsValid = true;
                                }
                            }
//if already match found break from here as well
if(args.IsValid == false) break;

                        }


如果您使用的是.net 3.5及以上版本:



If you're using .net 3.5 and above:

sl = (string[])sl.Distinct().ToArray(typeof(string));





这种方式你只处理不同的值



如果警告用户很重要,那么你创建新的数组并比较sl和这个新数组的长度。





This way you're only processing distinct values

If it is important to warn the user, then you create new Array and compare length of sl and this new array.

String[] sl_distinct = (string[])sl.Distinct().ToArray(typeof(string));
if (sl.length != sl_distinct.length){
// your message
}