/// <summary>
/// 分割url,如cms/channel/add/index.aspx,将被分割 为/,/cms,/cms/channel,/cms/channel/add,/cms/channel/add/index.aspx;
/// </summary>
/// <param name="url">要分割的URL</param>
/// <returns>发挥分割后的结果</returns>
public static List<string> SplitUrl(string url)
{
string[] urlList = url.Split('/');
List<string> urlDiy = new List<string>();//存放分割后,为每个地址加上"/"的地址
urlDiy.Add("/");//第一个默认为"/"
List<string> directory = new List<string>();//存放分割后的地址
for (int j = 0; j < urlList.Length; j++)//为分割后的地址加上"/"
{ urlDiy.Add("/" + urlList[j]); }
for (int i = 0; i < urlDiy.Count; i++)//重新组合分割后的地址,如:/web,/images,组合为/web/images
{ if (i == 0 || i == 1)
{
directory.Add(urlDiy[i]);
}
else
{
directory.Add(directory[i - 1] + urlDiy[i]);
} }
return directory;
}
大家看看这段代码有没问题,是不是有更好的写法?
/// 分割url,如cms/channel/add/index.aspx,将被分割 为/,/cms,/cms/channel,/cms/channel/add,/cms/channel/add/index.aspx;
/// </summary>
/// <param name="url">要分割的URL</param>
/// <returns>发挥分割后的结果</returns>
public static List<string> SplitUrl(string url)
{
string[] urlList = url.Split('/');
List<string> urlDiy = new List<string>();//存放分割后,为每个地址加上"/"的地址
urlDiy.Add("/");//第一个默认为"/"
List<string> directory = new List<string>();//存放分割后的地址
for (int j = 0; j < urlList.Length; j++)//为分割后的地址加上"/"
{ urlDiy.Add("/" + urlList[j]); }
for (int i = 0; i < urlDiy.Count; i++)//重新组合分割后的地址,如:/web,/images,组合为/web/images
{ if (i == 0 || i == 1)
{
directory.Add(urlDiy[i]);
}
else
{
directory.Add(directory[i - 1] + urlDiy[i]);
} }
return directory;
}
大家看看这段代码有没问题,是不是有更好的写法?
public static List<string> SplitUrl(string url)
{
string[] urlList = url.Split('/');
List<string> urlDiy = new List<string>();//存放分割后,为每个地址加上"/"的地址
urlDiy.Add("/");//第一个默认为"/"
List<string> directory = new List<string>();//存放分割后的地址 for (int i = 0; i != urlList.Length; i++)
{
if (urlDiy.Count == 1)
{
urlDiy.Add(urlDiy[urlDiy.Count - 1] + urlList[i]);
}
else
{
urlDiy.Add(urlDiy[urlDiy.Count - 1] + "/" + urlList[i]);
}
}
return directory;
}???这个意思?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace ConsoleApplication1
{
class Program
{
/// <summary>
/// 分割url,如cms/channel/add/index.aspx,将被分割 为/,/cms,/cms/channel,/cms/channel/add,/cms/channel/add/index.aspx;
/// </summary>
/// <param name="url">要分割的URL</param>
/// <returns>发挥分割后的结果</returns>
public static string[] SplitUrl(string url)
{
string[] urlHead = new string[] { "http://", "https://" }; // 删除参数部位
int n = url.IndexOf('?');
if (n >= 0)
{
url = url.Substring(0, n);
} for (int i = 0; i < urlHead.Length; i++)
{
if (url.StartsWith(urlHead[i]))
{
url = url.Substring(urlHead[i].Length); //去掉头
n = url.IndexOf('/');
if (n > 0)
{
url = url.Substring(n);// 去域名,保留 '/'
}
else
{
url = "";
}
break;
}
}
if (url.Length == 0)
{
return new string[0];
}
if (url[0] != '/')
{
url = '/' + url;
}
url = url.TrimEnd('/'); string[] urlList = url.Split(new char[]{'/'}, StringSplitOptions.None);
urlList[0] = "";
for (int i = 1; i < urlList.Length; i++)//重新组合分割后的地址,如:/web,/images,组合为/web/images
{
urlList[i] = urlList[i - 1] +'/'+ urlList[i];
}
urlList[0] = "/";
return urlList;
}
static void print(string[] str)
{
for (int i = 0; i < str.Length; i++)
{
Console.WriteLine(str[i]);
}
Console.WriteLine("-----------------------------");
} static void Main(string[] args)
{
string[] test = new string[]{"",
"http://",
"http://www.abc.com",
"http://www.abc.com/channel/add",
"https://www.abc.com/cms/channel/add/",
"/cms/channel/add",
"cms/channel/add",
"/cms/channel/add/",
"cms/channel/add/index.aspx",}; foreach (string item in test)
{
Console.WriteLine("test value: " + item);
print(SplitUrl(item));
}
}
}
}