C# 基于“/”分割字符串(Uri的路径)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17413106/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 09:25:59  来源:igfitidea点击:

Split string (path of Uri) based on "/"

c#asp.netquery-string

提问by SxChoc

Wonder if someone could point me in the right direction. What I'd like to achieve is to split a string based upon it having a '/' in it. For example if I had: www.site.com/course/123456/216 in code (c#) I'd like to be able to split the string in code so that 123456 could be assigned to variable param1 and 216 be assigned to param2 (course is the 'friendly' name of the page). If I was to add a third '/' on the string I'd like this to become param3, etc, etc.

想知道是否有人可以指出我正确的方向。我想要实现的是根据其中包含“/”的字符串拆分字符串。例如,如果我有: www.site.com/course/123456/216 in code (c#) 我希望能够在代码中拆分字符串,以便 123456 可以分配给变量 param1 和 216 分配给 param2 (course 是页面的“友好”名称)。如果我要在字符串上添加第三个“/”,我希望它成为 param3 等。

Ideally I'd like to be able to put this code somewhere that I could include it on whichever usercontrols I'd need it to work.

理想情况下,我希望能够将此代码放在某个地方,以便我可以将其包含在我需要它工作的任何用户控件上。

回答by Smeegs

Why not just use split?

为什么不直接使用split?

var valueArray = "www.site.com/course/123456/216".Split('/');

The array will have the entire string broken up

该数组将使整个字符串分解

index 0 would be "www.site.com" and so forth.

索引 0 将是“www.site.com”等等。

回答by Alfie

http://msdn.microsoft.com/en-us/library/b873y76a.aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-1

http://msdn.microsoft.com/en-us/library/b873y76a.aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-1

string QueryString = "1234/567/890"; string[] QueryArray = QueryString.Split('/');

string QueryString = "1234/567/890"; string[] QueryArray = QueryString.Split('/');

Now QueryArray[0] = 1234, QueryArray[1] = 567, QueryArray[2] = 890,

现在QueryArray[0] = 1234, QueryArray[1] = 567, QueryArray[2] = 890,

回答by Mike Perrenoud

Well, making the assumptionthat the values would nothave /in them:

好吧,假设这些值不会包含/在其中:

var splitVals = queryString.Split('/');
var vals = new Dictionary<string, string>();
for (int i = 2; i <= splitVals.Count; i++)
{
    vals.Add(string.Format("param{0}", i), vals[i]);
}

That would get you started. Now, if you're looking to set them to real variables then you'd need to do some casting and leverage reflection, but your question isn't near clear enough to make any real assumptions there.

那会让你开始。现在,如果您希望将它们设置为实际变量,那么您需要进行一些转换和利用反射,但是您的问题还不够清楚,无法在那里做出任何实际假设。

EDIT

编辑

To make this code reusable I would build an extension method:

为了使这段代码可重用,我将构建一个扩展方法:

namespace System
{
    public static class StringExtensions
    {
        public static Dictionary<string, string> SplitQueryString(this string queryString)
        {
            var splitVals = queryString.Split('/');
            var vals = new Dictionary<string, string>();
            for (int i = 2; i <= splitVals.Count; i++)
            {
                vals.Add(string.Format("param{0}", i), vals[i]);
            }

            return vals;
        }
    }
}

because then you could do this:

因为那时你可以这样做:

var vals = queryString.SplitQueryString();

回答by Robert McKee

HttpContext.Current.Request.Url.AbsolutePath.Split('/')

回答by Alexei Levenkov

Uri.Segmentsmaybe what you are looking for:

Uri.Segments也许你正在寻找什么:

new Uri("http://www.contoso.com/foo/bar/index.htm#search").Segments

Results in [ "/", "foo/", "bar/", "index.html" ]

结果是 [ "/", "foo/", "bar/", "index.html" ]

回答by Max Hodges

url: server/func2/SubFunc2

网址: server/func2/SubFunc2

// Get path components. Trailing separators. Returns { "/", "func2/", "sunFunc2" }. string[] pathsegments = myUri.Segments;

// 获取路径组件。尾随分隔符。返回 { "/", "func2/", "sunFunc2" }。 string[] pathsegments = myUri.Segments;