我是 C# 新手,
假设我有一个字符串
string testurl = "http://www.mytestsite.com/hello";
if (test url == root domain) {
// do something
}
我想检查该字符串“testurl”是否是根域,即 http://www.mytestsite.com或http://mytestsite.com等等
谢谢。
最佳答案
使用Uri
类:
var testUrl = new Uri("http://www.mytestsite.com/hello");
if (testUrl.AbsolutePath== "/")
{
Console.WriteLine("At root");
}
else
{
Console.WriteLine("Not at root");
}
它很好地处理了可能需要的任何规范化问题(例如处理 http://www.mytestsite.com
和 http://www.mytestsite.com/
相同)
关于c# - 检查字符串中是否为根域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22859924/