Reputation: 4231
I have this string :
string resultString="section[]=100§ion[]=200§ion[]=300§ion[]=400";
I just want the numbers to be stored in the array result[]
like
result[0]=100
result[1]=200
result[3]=300
result[4]=400
Can anyone help me with this.
Upvotes: 3
Views: 395
Reputation: 35126
str.Split('&')
.Select(s=>s.Split('=')
.Skip(1)
.FirstOrDefault()).ToArray();
OR
str.Split(new[] { "section[]=" }, StringSplitOptions.RemoveEmptyEntries)
.Select(s => s.Replace("&", ""))
.Select(Int32.Parse).ToArray();
OR
var items = new List<string>();
foreach (Match item in Regex.Matches(str, @"section\[\]=(\d+)"))
items.Add(item.Groups[1].Value);
Upvotes: 2
Reputation: 18633
How about this?
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();
Upvotes: 2
Reputation: 2129
string x = "section[]=100§ion[]=200§ion[]=300§ion[]=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);
}
Upvotes: 1
Reputation: 1038780
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"
Upvotes: 7