说我有一个随机网址列表,网址已完全格式化:
http://www.example1.com,
http://example2.biz,
http://www.example3.co.uk
我有一个文本框的url值和urlSufix的dropDownList
因此用户正在插入网址:
标签:www.
,textBox [insert the domainNameOnly]
,DDL [.com, .biz, .co.uk, .net, .info]
并说出用户键入“example3”并选择了“.biz”
所以我的价值是"example3.biz"
我需要一种方法来将列表中的网址与用户输入进行比较
我以为我会分裂字符串('。')
所以选项是 对于前缀:
www.example
或只是
example
为sufix
example.com \ biz \net \info -> (2 parts)
example.co.uk \org.uk (3 parts)
所以有很多选择
可以是aray有2个元素的前缀
www.example
和sufix with 3 example.co.uk
以这种方式检查是很复杂的,
我用comparisson techniq选择错误的路径来分割Dots的网址吗?
我会告诉你我是如何开始的,一旦我发现它太多就停止了
这不是涵盖所有选项,已经太复杂了
if (ListArr[0] != "www")
{ // it means no www so
compare userArr[0] to ListArr[0] // to check domainMatch
// and for suffix of domain
var DDLValueArr = DDLValue.split('.');
if(DDLValueArr.length > 1) means it is co.uk or org.uk etc'
{
compare DDLValueArr[0] to ListArr[1]
}
}
else
compare userArr[0] to ListArr[1] cause the url in the List starts with "www"
比较用户输入到网址列表的好方法是什么?
答案 0 :(得分:2)
您可以使用Uri
class和UriBuilder
来创建uri,例如:
List<Uri> uris = new List<Uri>(){
new Uri("http://www.example1.com"),
new Uri("https://www.example2.biz"),
new Uri("http://www.example3.co.uk")
};
string input = "www.example2.biz";
Uri newUri = new UriBuilder(input).Uri;
if (uris.Any(u=> u.Host == newUri.Host))
{
MessageBox.Show("Already in the list");
}
请注意,如果uri未指定方案,则UriBuilder
默认为“http:”。