tags:

views:

524

answers:

3

I have a string aBc12def6 i want to convert the string into a pattern template (say aBc##def#) then generate values to place into the blank spaces (ex aBc00def0, aBc00def1 ... aBc25def9 ...). I dont want to do it all at once. I want to generate it one at a time and test the string. How do i do this? using C#

A: 

I don't know what the valid values for the # are, but I think you could do this in a few lines for code using a regular array.

For instance, if that last # can go from 0 to 9, give in length 9 for you array and store your string in there and give it the number array.Length minus the amount of array members that aren't yet filled in, meaning that aren't null.

That way, you get 0 through 9

But that's just assuming things, since I don't know what you can replace the # with. I'm assuming all number 0 to 9

WebDevHobo
correct for this prj i'll only need it to be numerical. I dont want to fill an array. also i am unsure whats the easiest way to parse it. I could check the amount of blank values (#) in the string and have a loop from i to len but whats the easiest way to insert the values into the proper place.
acidzombie24
+2  A: 
 //                      012345678
const string pattern = "aBc##def#";

StringBuilder builder = new StringBuilder(pattern);

builder[3] = 1;
builder[4] = 2;
builder[8] = 3;
string str1 = builder.ToString();

builder[3] = 4;
builder[4] = 5;
builder[8] = 6;
string str2 = builder.ToString();

Or

 const string pattern = "abc{0}{1}def{2}";
string str1 = string.Format(pattern, 1, 2, 3);
string str2 = string.Format(pattern, 4, 5, 6);
Paul Ruane
Love the XP style solution for the test case :)
ck
props to you, Stringbuilder makes it easier indeed.
WebDevHobo
Interesting solutions.
acidzombie24
A: 

I ended up counting the amount of #'s in the string, generate a number with that amount "{0:D"+amount.ToString()+"}" then copied the src string to a destination string stoping at # and replacing it with the digit in that slot.

acidzombie24