如何剪切包含url的字符串并将其添加到数组中(How can I cut a string that contains a url and add it to array)

编程入门 行业动态 更新时间:2024-10-23 20:21:11
如何剪切包含url的字符串并将其添加到数组中(How can I cut a string that contains a url and add it to array)

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

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

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.. }

但它并不像我希望它那样做。

适用任何形式的帮助

I am building doing a custom breadcrumb and I want to each LinkButton unique commandArgument url.

I have a generic string variable that is a url. The name of each subweb can be different and as long as possible the hierchy is not limited.

the String variable could look like this:

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

what I would like to do is to split the string variable and add it to a array so it looks like this:

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

I have tried following code:

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

But it doesnt do like I want it to do.

Any kind of help is appriacted

最满意答案

您可以使用扩展方法和一些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();

结果在LINQPad :

在此处输入图像描述

You can use an extension method for that and some 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; }

Here is the usage:

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

Result in LINQPad:

enter image description here

更多推荐

本文发布于:2023-07-08 20:06:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1080284.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   组中   并将其   url   array

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!