【问题标题】:How to split and take multiple strings from a url in c#?如何从c#中的url拆分和获取多个字符串?
【发布时间】:2016-10-25 08:06:01
【问题描述】:

我有一个看起来像这样的字符串:

/Gender=&Age=&Query=&Orgrimmar+l%C3%A4n=01&Stormwind+l%C3%A4n=07&Undercity+l%C3%A4n=09&Pag

我想要一个包含“Orgrimmar”、“Stormwind”和“Undercity”的字符串列表。这怎么可能,以便它在查询后以及 & 和 + 之间拆分,以便我们避免得到像这样的字符串 "Orgrimmar+l%C3%A4n=01&Stormwind"。

假设我们不知道字符串的名称.. :)

已更新,我似乎仍然无法正常工作。我添加了一个县列表,我可以用来验证这一点。但是,在这种情况下,我仍然觉得很难。 CountyList 用于验证 url 中的县/市是否与预先存在的 Collection 匹配。

        var countyQuery = Request.Url.Query;
        var counties = this._locationService.GetAllCounties();
        List<string> countyList = new List<string>();
        List<string> selectedCountiesList = new List<string>();

        foreach (var i in counties)
        {
            countyList.Add(i.Name);

        }

        Regex r = new Regex(@"&(.+?)\+");
        MatchCollection mc = r.Matches(countyQuery);

        foreach (Match curMatch in mc)
        {
            if (countyList.Contains(curMatch.Groups[1].Value))
            {
                selectedCountiesList.Add(curMatch.Groups[1].Value);
            }
        }



        return selectedCountiesList;

将网址更改为/?Gender=&Age=&Query=&county=13&county=08&county=01&Page=1 其中 13、08、01 等是县的 ID 最终的解决方案是: var selectedCountyQuery = Request.QueryString //CountySearch = "县" [QueryStringParameters.CountySearch]; 列出countyList = new List();

        List<string> selectedCounties = new List<string>();
        if (!string.IsNullOrEmpty(selectedCountyQuery))
        {
            var selectedCountiesArray = selectedCountyQuery.Split(new[]{ ',' });
            foreach (var selectedCounty in selectedCountiesArray)
            {
                selectedCounties.Add(selectedCounty);
            }
        }
        return selectedCounties;

【问题讨论】:

  • 到目前为止你尝试过什么?字符串的相似度是什么??
  • 我已经尝试定义一个开始和结束索引。相似之处在于它们都以 & 开头并带有 +。 int startIndex = selectedCityQuery.IndexOf('&'); int endIndex = selectedCityQuery.IndexOf('+');
  • 请展示您尝试过的内容以及问题所在 - stackoverflow.com/help/on-topic

标签: c# url split


【解决方案1】:

您可以使用Substring()Split() 方法获取所有参数和值。

例子:

var URL = "controller/method?var1=&var2=&var3=dsgdf";

var ParameterPart = URL.Split("?")[1];

var ParametersArray = ParameterPart.Split("&");
//output : ["var1=","var2=","var3=dsgdf"];

foreach(var Parameter in ParametersArray)
{
   var ParameterName= Parameter.Split("=")[0];
   var ParameterValue= Parameter.Split("=")[1];
}

【讨论】:

  • 谢谢你!我会试试这个! :)
  • 是的,它奏效了。我采取了另一种方法,它奏效了。感谢您的提示,这很棒:)
【解决方案2】:

您可以使用正则表达式并提取匹配项:

Regex r = new Regex(@"&(.+?)\+");
MatchCollection mc = r.Matches(s);

然后你可以迭代你想要的字符串(在这种情况下哇城市)像:


    foreach(Match curMatch in mc) 
    {
        Console.WriteLine(curMatch.Groups[1].Value);
    }

【讨论】:

    【解决方案3】:
    string[] numbers ={ "/Gender=&Age=&Query=&Orgrimmar+l%C3%A4n=01&Stormwind+l%C3%A4n=07&Undercity+l%C3%A4n=09&Pag"};
    string sPattern = @"(?<=&Orgrimmar)+";
        foreach (string s in numbers){
    if (System.Text.RegularExpressions.Regex.IsMatch(s, sPattern)){
    System.Console.WriteLine(" - valid");}
    else{System.Console.WriteLine(" - invalid");}
    

    输出:有效

    string[] numbers ={ "/Gender=&Age=&Query=Orgrimmar+l%C3%A4n=01&Stormwind+l%C3%A4n=07&Undercity+l%C3%A4n=09&Pag"};
    

    输出:无效

    进一步检查两个参数:

        string[] numbers ={ "/Gender=&Age=&Query=&Orgrimmar+l%C3%A4n=01&Stormwind+l%C3%A4n=07&Undercity+l%C3%A4n=09&Pag"};
    
    string sPattern = @"(?<=&Orgrimmar)+";
    string sPattern2 = @"(?<=&Stormwind)+";
    foreach (string s in numbers){
    if (System.Text.RegularExpressions.Regex.IsMatch(s, sPattern) && System.Text.RegularExpressions.Regex.IsMatch(s, sPattern2))
    

    ...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-20
      • 2021-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多