正在将字符串解析为C#中的数组

本文关键字:数组 字符串 | 更新日期: 2023-09-27 18:11:09

我有这个字符串:

string resultString="section[]=100&section[]=200&section[]=300&section[]=400";

我只想把数字像一样存储在数组result[]

result[0]=100
result[1]=200
result[3]=300
result[4]=400

有人能帮我吗。

正在将字符串解析为C#中的数组

NameValueCollection values = HttpUtility.ParseQueryString("section[]=100&section[]=200&section[]=300&section[]=400");
string[] result = values["section[]"].Split(',');
// at this stage 
// result[0] = "100"
// result[1] = "200"
// result[2] = "300"
// result[3] = "400"

这个怎么样?

        string s ="section[]=100&section[]=200&section[]=300&section[]=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();
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);
string x = "section[]=100&section[]=200&section[]=300&section[]=400";
// Split into a list
string[] vals = x.Split('&');
List<int> results = new List<int>();
foreach (string aResult in vals)
{
    int result = 0;
    string[] val = aResult.Split('=');
    // Get right hand side
    if (val.Length == 2 && int.TryParse(val[1], out result))
        results.Add(result);                    
}