【发布时间】: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