Reputation: 2417
string str = "XXX_123_456_789_ABC";
int[] intAry = GetIntAryByStr(str);
Get int[] result
like this int[0] <- 123 , int[1] <- 456 , int[2] <- 789
string str = "A111B222C333.bytes";
int[] intAry = GetIntAryByStr(str);
Get int[] result
like this int[0] <- 111, int[1] <- 222 , int[2] <- 333
How to do it !?
Upvotes: 1
Views: 117
Reputation: 4355
Just to demonstrate what @Klaus Gütter suggests, and with linq:
static int[] GetIntAryByStr(string s)
{
return Regex.Matches(s, @"\d+")
.OfType<Match>()
.Select(x => Convert.ToInt32(x.Value))
.ToArray();
}
Upvotes: 2
Reputation: 186833
You can try regular expressions in order to match all items:
using System.Linq;
using System.Text.RegularExpressions;
...
int[] intAry = Regex
.Matches(str, "[0-9]+")
.Cast<Match>()
.Select(match => int.Parse(match.Value))
.ToArray();
If array items must have 3
digits only, change [0-9]+
pattern into [0-9]{3}
Upvotes: 4