how do i extract integer values in an int array from this string
1840 1841
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Kirtan PatelRank #292 · 8.6K reputationPosted Mar 5, 2010, 5:01 PM EST
using System.Text.RegularExpressions;
string Data = "1840 1841 ";
//Get All Integer In Array
Regex rex = new Regex("[0-9]+");
int[] Array = new int[rex.Matches(Data).Count];
int i=0;
foreach (Match m in rex.Matches(Data))
{
Array[i++] = int.Parse(m.Value.ToString());
}
theLizardRank #1,079 · 1.8K reputationPosted Mar 5, 2010, 12:24 PM EST
private void doTest()
{
int[] iArr = new int[0];
for(int i=0; i < test.Length; i++)
{
if( Char.IsDigit(test, i) )
{
Array.Resize(ref iArr, iArr.Length+1);
iArr[iArr.Length-1] = int.Parse(test[i].ToString());
}
}
}
string manipulation is a very important quality.
Sam HobbsRank #107 · 20.5K reputationPosted Mar 5, 2010, 11:40 PM EST
Note that XmlSerializer can read from a StringReader stream instead of a file stream if you need to read from memory instead of storage.
Desi Gal35 reputationPosted Mar 8, 2010, 5:47 AM EST
@theLizard:your answer separates out individual digit in an array,thanks though.
@Sam:thanks ,I will try out ur solution
Sam HobbsRank #107 · 20.5K reputationPosted Mar 8, 2010, 11:04 AM EST
I am not sure what happened to the attachment to my previous post, but I tried a few times to upload it again. I will try again to upload it.