【问题标题】:How can I cut a string that contains a url and add it to array如何剪切包含 url 的字符串并将其添加到数组
【发布时间】:2014-03-25 10:18:12
【问题描述】:

我正在构建一个自定义面包屑,我想要每个 LinkBut​​ton 唯一的 commandArgument url。

我有一个通用字符串变量,它是一个 url。每个子网站的名称可以不同,并且尽可能不限制层次结构。

String 变量可能如下所示:

http://site/org/content/Change/Book/process/item

我想做的是拆分字符串变量并将其添加到数组中,如下所示:

http://site/org
http://site/org/content/
http://site/org/content/Change/
http://site/org/content/Change/Book/
http://site/org/content/Change/Book/process/
http://site/org/content/Change/Book/process/item

我尝试了以下代码:

 private void AddBreadCrumb(SPWeb web)
    {
     var webUrl = web.Url;
     var linkList = new List<string>(webUrl.Split('/'));
     // etc..
    }

但它不像我想要的那样做。

任何形式的帮助都是appriacted

【问题讨论】:

  • 如果 URI 路径以 / 结尾,您希望发生什么?

标签: c# asp.net arrays string split


【解决方案1】:

您可以为此使用扩展方法和一些LINQ

public static IEnumerable<string> ParseUrl(this string source)
{
    if(!Uri.IsWellFormedUriString(source, UriKind.Absolute)) 
         throw new ArgumentException("The URI Format is invalid");

    var index = source.IndexOf("//");
    var indices = source.Select((x, idx) => new {x, idx})
                .Where(p => p.x == '/' && p.idx > index + 1)
                .Select(p => p.idx);

    // Skip the first index because we don't want http://site
    foreach (var idx in indices.Skip(1))
    {
       yield return source.Substring(0,idx);
    }
    yield return source;
}

用法如下:

string url = "http://site/org/content/Change/Book/process/item";
var parts = url.ParseUrl();

结果为@​​987654325@:

【讨论】:

  • 应该使用 var parts = ParseUrl(url); ?
  • @Obsivus 你也可以这样称呼它。但这是一种扩展方法,为了使用this关键字,你需要将该代码放入publicstatic类中。
  • 感谢您的精彩回答!
  • 使用yield关键字会避免结果列表的分配。
  • @Jodrell 谢谢你的好建议!我更新了我的答案。
【解决方案2】:

如果我理解您的问题,您希望执行以下操作。请注意使用框架中内置的Uri 类。

如果需要,您可以按键值对进一步细分查询字符串。

var breadCrumbs = 
    EnumerateBreadCrumbs(@"http://site/org/content/Change/Book/process/item")
    .Select(uri => uri.ToString())
    .ToArray();


private IEnumerable<Uri> EnumerateBreadCrumbs(string url)
{
    var raw = new Uri(url);

    var buffer = new UriBuilder(raw.Scheme, raw.Host, raw.Port);
    yield return buffer.Uri;

    for (var i = 1; i < raw.Segements.Length; i++)
    {
        if (raw.Segments[i] == "/")
        {
            continue;
        }

        buffer.Path += raw.Segments[i];
        yield return buffer.Uri;
    }

    if (raw.Query.Length > 1)
    {
        buffer.Query = new string(raw.Query.Skip(1).ToArray());
        yield return buffer.Uri;
    }

    if (raw.Fragment.Length < 2)
    {
        yield break;
    }

    buffer.Fragment = new string(raw.Fragment.Skip(1).ToArray());
    yield return buffer.Uri;
}

【讨论】:

    【解决方案3】:

    你可以用你自己的方法,但是先剪掉url字符串的第一部分:

    var webUrl=web.Url;
    webUrl=webUrl.SubString(7);//removing "http://"
    var linkList=new List<string>(webUrl.Split('/'));
    

    【讨论】:

    • web.Url = "https://www.linkedin.com/"; web.Url = "ftp://ftp.microsoft.com/pub";即时失败。
    猜你喜欢
    • 1970-01-01
    • 2014-12-02
    • 2017-08-03
    • 2017-01-08
    • 2014-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多