我有这个字符串:
string resultString="section[]=100§ion[]=200§ion[]=300§ion[]=400";我只希望将数字存储在数组result[]中,如下所示
result[0]=100
result[1]=200
result[3]=300
result[4]=400有没有人能帮我。
发布于 2011-10-13 00:34:23
NameValueCollection values = HttpUtility.ParseQueryString("section[]=100§ion[]=200§ion[]=300§ion[]=400");
string[] result = values["section[]"].Split(',');
// at this stage
// result[0] = "100"
// result[1] = "200"
// result[2] = "300"
// result[3] = "400"发布于 2011-10-13 00:35:07
str.Split('&')
.Select(s=>s.Split('=')
.Skip(1)
.FirstOrDefault()).ToArray();或
str.Split(new[] { "section[]=" }, StringSplitOptions.RemoveEmptyEntries)
.Select(s => s.Replace("&", ""))
.Select(Int32.Parse).ToArray();或
var items = new List<string>();
foreach (Match item in Regex.Matches(str, @"section\[\]=(\d+)"))
items.Add(item.Groups[1].Value);发布于 2011-10-13 00:39:44
这个怎么样?
string s ="section[]=100§ion[]=200§ion[]=300§ion[]=400";
Regex r = new Regex(@"section\[\]=([0-9]+)(&|$)");
List<int> v = new List<int>();
Match m=r.Match(s);
while (m.Success){
v.Add(Int32.Parse(m.Groups[1].ToString()));
m=m.NextMatch();
}
int[]result = v.ToArray();https://stackoverflow.com/questions/7743341
复制相似问题