我怎样才能获得以下字符串的一部分?

时间:2022-05-26 06:45:55

Lets say I have an absolute url /testserver/tools/search.aspx that I store in a variable url.

假设我有一个绝对的url /testserver/tools/search.aspx,我存储在一个变量url中。

I want to check if url == /tools/search.aspx without having to use /testserver.

我想检查是否url == /tools/search.aspx而不必使用/ testserver。

A more complete example would be:

一个更完整的例子是:

My testserver contains the url http://www.testserver.com/tools/Search.aspx,

我的testserver包含url http://www.testserver.com/tools/Search.aspx,

but my live server contains the url http://www.liveserver.com/tools/Search.aspx

但我的实时服务器包含网址http://www.liveserver.com/tools/Search.aspx

If I compare a variable url which stores the testserver url to the liveserver url, it will fail, thats why I want to just check the /tools/Search.aspx portion.

如果我将存储testserver url的变量url与liveserver url进行比较,它将失败,这就是为什么我只想检查/tools/Search.aspx部分。

5 个解决方案

#1


2  

if (url.ToLower().Contains("/tools/search.aspx"))
{
   //do stuff here
}

I would use Contains in case you have a query string, but you could also use EndsWith("/tools/search.aspx") if you don't have query strings.

如果您有查询字符串,我会使用Contains,但如果您没有查询字符串,也可以使用EndsWith(“/ tools / search.aspx”)。

#2


3  

If your input is of the form "http://www.testserver.com/tools/Search.aspx":

如果您的输入格式为“http://www.testserver.com/tools/Search.aspx”:

var path1 = new Uri("http://www.testserver.com/tools/Search.aspx").AbsolutePath;
var path2 = new Uri("http://www.liveserver.com/tools/Search.aspx").AbsolutePath;

Both result in "/tools/Search.aspx".

两者都会产生“/tools/Search.aspx”。

Using the Uri is the best solution if you have to accept any URI, i.e. including those with a query string, fragment identifiers, etc.

如果您必须接受任何URI,即使用包含查询字符串,片段标识符等的URI,则使用Uri是最佳解决方案。


If your input is of the form "/testserver.com/tools/Search.aspx" and you know that all input will always be of this form and valid and contain no other URI components:

如果您的输入格式为“/testserver.com/tools/Search.aspx”,并且您知道所有输入将始终为此格式且有效且不包含其他URI组件:

var input = "/testserver.com/tools/Search.aspx";
var path1 = input.Substring(input.Index('/', 1));

Result is "/tools/Search.aspx".

结果是“/tools/Search.aspx”。

#3


1  

Regex.Match(url, @"^.*?/tools/search\.aspx\??.*",
                 RegexOptions.IgnoreCase).Success == true

If you grab url from Request.PathInfo you won't have the domain anyway...but you question is ambiguous, as you say your have a path /testserver/ in one but not in the urls you provide.

如果您从Request.PathInfo获取网址,则无论如何都不会拥有该网址...但您的问题是不明确的,因为您说您的路径/ testserver /在一个路径中,但在您提供的网址中没有。

Otherwise, set url from Request.Url.ToString()

否则,从Request.Url.ToString()设置url

#4


0  

You can use the property AppRelativeCurrentExecutionFilePath, which will give you the path relative the application root. So "http://host/virtualfolder/page.aspx" will be "~/page.aspx":

您可以使用属性AppRelativeCurrentExecutionFilePath,它将为您提供相对应用程序根目录的路径。所以“http://host/virtualfolder/page.aspx”将是“〜/ page.aspx”:

if (HttpContext.Current.Request.AppRelativeCurrentExecutionFilePath.Equals("~/search.aspx", StringComparison.InvariantCultureIgnoreCase))
{
    // do something
}

#5


0  

If the only difference is going to be the host part of the URL I would use the System.Uri class to compare their absolute paths (the "tools/Search.aspx" part of the uri).

如果唯一的区别是URL的主机部分,我将使用System.Uri类来比较它们的绝对路径(uri的“tools / Search.aspx”部分)。

Here's an example of how to do it:

以下是如何执行此操作的示例:

static void Main(string[] args)
{
    //load up the uris
    Uri uri = new Uri("http://www.testserver.com/tools/search.aspx");            
    Uri matchingUri = new Uri("http://www.liveserver.com/tools/search.aspx");
    Uri nonMatchingUri = new Uri("http://www.liveserver.com/tools/cart.aspx");

    //demonstrate what happens when the uris match
    if (uri.AbsolutePath == matchingUri.AbsolutePath)
        Console.WriteLine("These match");
    //demonstrate what happens when the uris don't match
    if (uri.AbsolutePath != nonMatchingUri.AbsolutePath)
        Console.WriteLine("These do not match");
}

#1


2  

if (url.ToLower().Contains("/tools/search.aspx"))
{
   //do stuff here
}

I would use Contains in case you have a query string, but you could also use EndsWith("/tools/search.aspx") if you don't have query strings.

如果您有查询字符串,我会使用Contains,但如果您没有查询字符串,也可以使用EndsWith(“/ tools / search.aspx”)。

#2


3  

If your input is of the form "http://www.testserver.com/tools/Search.aspx":

如果您的输入格式为“http://www.testserver.com/tools/Search.aspx”:

var path1 = new Uri("http://www.testserver.com/tools/Search.aspx").AbsolutePath;
var path2 = new Uri("http://www.liveserver.com/tools/Search.aspx").AbsolutePath;

Both result in "/tools/Search.aspx".

两者都会产生“/tools/Search.aspx”。

Using the Uri is the best solution if you have to accept any URI, i.e. including those with a query string, fragment identifiers, etc.

如果您必须接受任何URI,即使用包含查询字符串,片段标识符等的URI,则使用Uri是最佳解决方案。


If your input is of the form "/testserver.com/tools/Search.aspx" and you know that all input will always be of this form and valid and contain no other URI components:

如果您的输入格式为“/testserver.com/tools/Search.aspx”,并且您知道所有输入将始终为此格式且有效且不包含其他URI组件:

var input = "/testserver.com/tools/Search.aspx";
var path1 = input.Substring(input.Index('/', 1));

Result is "/tools/Search.aspx".

结果是“/tools/Search.aspx”。

#3


1  

Regex.Match(url, @"^.*?/tools/search\.aspx\??.*",
                 RegexOptions.IgnoreCase).Success == true

If you grab url from Request.PathInfo you won't have the domain anyway...but you question is ambiguous, as you say your have a path /testserver/ in one but not in the urls you provide.

如果您从Request.PathInfo获取网址,则无论如何都不会拥有该网址...但您的问题是不明确的,因为您说您的路径/ testserver /在一个路径中,但在您提供的网址中没有。

Otherwise, set url from Request.Url.ToString()

否则,从Request.Url.ToString()设置url

#4


0  

You can use the property AppRelativeCurrentExecutionFilePath, which will give you the path relative the application root. So "http://host/virtualfolder/page.aspx" will be "~/page.aspx":

您可以使用属性AppRelativeCurrentExecutionFilePath,它将为您提供相对应用程序根目录的路径。所以“http://host/virtualfolder/page.aspx”将是“〜/ page.aspx”:

if (HttpContext.Current.Request.AppRelativeCurrentExecutionFilePath.Equals("~/search.aspx", StringComparison.InvariantCultureIgnoreCase))
{
    // do something
}

#5


0  

If the only difference is going to be the host part of the URL I would use the System.Uri class to compare their absolute paths (the "tools/Search.aspx" part of the uri).

如果唯一的区别是URL的主机部分,我将使用System.Uri类来比较它们的绝对路径(uri的“tools / Search.aspx”部分)。

Here's an example of how to do it:

以下是如何执行此操作的示例:

static void Main(string[] args)
{
    //load up the uris
    Uri uri = new Uri("http://www.testserver.com/tools/search.aspx");            
    Uri matchingUri = new Uri("http://www.liveserver.com/tools/search.aspx");
    Uri nonMatchingUri = new Uri("http://www.liveserver.com/tools/cart.aspx");

    //demonstrate what happens when the uris match
    if (uri.AbsolutePath == matchingUri.AbsolutePath)
        Console.WriteLine("These match");
    //demonstrate what happens when the uris don't match
    if (uri.AbsolutePath != nonMatchingUri.AbsolutePath)
        Console.WriteLine("These do not match");
}