问题描述:

www.google.com/mywork-part1/project1

www.google.com/workspace/project1

How can I split url by asp.net

IN Javascript I can do like

var getURL = 'www.google.com/mywork-part1/project1';

var newURL = getURL.split("/")[2];

but How I can do it using asp.net

also is there a way to do like "mywork-part1" to remove "-" to get the value only "mywork" so if has "-" then replace by "space"

a noobie of asp, Many thanks

网友答案:
Uri uri = new Uri("http://www.myDomain.com/someDir/anotherDir");
string dirs = uri.GetComponents(UriComponents.Path, UriFormat.SafeUnescaped);
string[] dirArray = dirs.Split("/");
for (int i = 0; i <= dirArray.Length - 1; i++) {
    Trace.Warn(string.Format("Directory {0} is at index {1}", dirArray[i].Replace("-",String.Empty), i.ToString()));
    // find each directory in the path with its corresponding index number '
}
    // reference someDir directly '
Trace.Warn(dirArray[0]);

Ref: http://www.codepal.co.uk/show/Using_RequestUrl_to_find_specific_parts_of_the_web_pages_address

网友答案:

easy way

string Url = "www.google.com/mywork-part1/project1";
string[] newURL = Url.Split('/');
string result = newURL[1].Replace("-", " ");

Response.Write(result);
相关阅读:
Top