Passing an argument list as a single parameter

  • Thread starter Thread starter Craig Dunstan
  • Start date Start date
C

Craig Dunstan

I would like to dynamically build a list of arguments to pass the
string.Format method and I am not sure how to do it.

Basically, I would like to dynamically build a list based on the results of
a regular expression match ie:

private string[] FormatContent(string pContent, string pExpression, pFormat)
{
Regex p = new Regex(pExpression, RegexOptions.SingleLine);
MatchCollection matches = p.Matches(pContent);

string[] result = new string[matches.Count];
for (int i=0; i<matches.Count; i++)
{
// build a dynamic params list to pass to the format, let's call it
"paramlist" from
for (int j=0; j<matches.Groups.Count; j++)
{
// add matches.Groups[j].Value to the dynmic paramlist...
????
}
result = string.Format(pFormat, ?paramlist?)
}

// now give the result back.....
return( result );
}
 
Hi Craig,

Won't the string.Format(<string>, <object[]>) overload work?

You just need to create an array of arguments.

HTH,
Rakesh Rajan
 
so, taking the example and extending it....

string[] paramlist = new string[matches.Groups.Count];
for (int j=0; j<matches.Groups.Count; j++)
{
// add matches.Groups[j].Value to the dynmic paramlist...
paramlist[j] = matches.Groups[j].Value;
}
result = string.Format(pFormat, paramlist);
}


will this do the trick if pFormat contains something like "{0}/{1}/{2}" and
there are 3 groups in the expression?

Rakesh Rajan said:
Hi Craig,

Won't the string.Format(<string>, <object[]>) overload work?

You just need to create an array of arguments.

HTH,
Rakesh Rajan

Craig Dunstan said:
I would like to dynamically build a list of arguments to pass the
string.Format method and I am not sure how to do it.

Basically, I would like to dynamically build a list based on the results
of
a regular expression match ie:

private string[] FormatContent(string pContent, string pExpression,
pFormat)
{
Regex p = new Regex(pExpression, RegexOptions.SingleLine);
MatchCollection matches = p.Matches(pContent);

string[] result = new string[matches.Count];
for (int i=0; i<matches.Count; i++)
{
// build a dynamic params list to pass to the format, let's call it
"paramlist" from
for (int j=0; j<matches.Groups.Count; j++)
{
// add matches.Groups[j].Value to the dynmic paramlist...
????
}
result = string.Format(pFormat, ?paramlist?)
}

// now give the result back.....
return( result );
}
 
Hi Craig,

Absolutely.

Find below a sample code for this (it's a different context though):

Console.WriteLine("Number of params: ");
int val = int.Parse(Console.ReadLine());

string[] arguments = new string[val];

for(int x=0; x<val; x++)
{
Console.Write("Val " + x.ToString() + ": ");
arguments[x] = Console.ReadLine();
}

string output = string.Format("{0}/{1}/{2}", arguments);

Console.WriteLine(output);

HTH,
Rakesh Rajan

Craig Dunstan said:
so, taking the example and extending it....

string[] paramlist = new string[matches.Groups.Count];
for (int j=0; j<matches.Groups.Count; j++)
{
// add matches.Groups[j].Value to the dynmic paramlist...
paramlist[j] = matches.Groups[j].Value;
}
result = string.Format(pFormat, paramlist);
}


will this do the trick if pFormat contains something like "{0}/{1}/{2}" and
there are 3 groups in the expression?

Rakesh Rajan said:
Hi Craig,

Won't the string.Format(<string>, <object[]>) overload work?

You just need to create an array of arguments.

HTH,
Rakesh Rajan

Craig Dunstan said:
I would like to dynamically build a list of arguments to pass the
string.Format method and I am not sure how to do it.

Basically, I would like to dynamically build a list based on the results
of
a regular expression match ie:

private string[] FormatContent(string pContent, string pExpression,
pFormat)
{
Regex p = new Regex(pExpression, RegexOptions.SingleLine);
MatchCollection matches = p.Matches(pContent);

string[] result = new string[matches.Count];
for (int i=0; i<matches.Count; i++)
{
// build a dynamic params list to pass to the format, let's call it
"paramlist" from
for (int j=0; j<matches.Groups.Count; j++)
{
// add matches.Groups[j].Value to the dynmic paramlist...
????
}
result = string.Format(pFormat, ?paramlist?)
}

// now give the result back.....
return( result );
}

 
Thanks very much for your help - I am pretty new to C# and that was easier
than I expected!
For anyone interested, this is the bit of code i ended up with. It shows use
of Regex also.....note that the number of groups in the regular expression
must be greater than or equal to the numer of format entries or there will
be an exception raised.....

private string[] GetMatches(string content, string expression, string
format)
{
Regex p = new Regex(expression, RegexOptions.Singleline);
MatchCollection matches = p.Matches(content);
string[] results = new string[matches.Count];
for (int i=0; i<matches.Count; i++)
{
string[] paramlist = new string[matches.Groups.Count];
for (int j=0; j<matches.Groups.Count; j++)
{
paramlist[j] = matches.Groups[j].Value;
}
results = string.Format(format, paramlist);
}
return ( results );
}

Rakesh Rajan said:
Hi Craig,

Absolutely.

Find below a sample code for this (it's a different context though):

Console.WriteLine("Number of params: ");
int val = int.Parse(Console.ReadLine());

string[] arguments = new string[val];

for(int x=0; x<val; x++)
{
Console.Write("Val " + x.ToString() + ": ");
arguments[x] = Console.ReadLine();
}

string output = string.Format("{0}/{1}/{2}", arguments);

Console.WriteLine(output);

HTH,
Rakesh Rajan

Craig Dunstan said:
so, taking the example and extending it....

string[] paramlist = new string[matches.Groups.Count];
for (int j=0; j<matches.Groups.Count; j++)
{
// add matches.Groups[j].Value to the dynmic paramlist...
paramlist[j] = matches.Groups[j].Value;
}
result = string.Format(pFormat, paramlist);
}


will this do the trick if pFormat contains something like "{0}/{1}/{2}"
and
there are 3 groups in the expression?

Rakesh Rajan said:
Hi Craig,

Won't the string.Format(<string>, <object[]>) overload work?

You just need to create an array of arguments.

HTH,
Rakesh Rajan

:

I would like to dynamically build a list of arguments to pass the
string.Format method and I am not sure how to do it.

Basically, I would like to dynamically build a list based on the
results
of
a regular expression match ie:

private string[] FormatContent(string pContent, string pExpression,
pFormat)
{
Regex p = new Regex(pExpression, RegexOptions.SingleLine);
MatchCollection matches = p.Matches(pContent);

string[] result = new string[matches.Count];
for (int i=0; i<matches.Count; i++)
{
// build a dynamic params list to pass to the format, let's call
it
"paramlist" from
for (int j=0; j<matches.Groups.Count; j++)
{
// add matches.Groups[j].Value to the dynmic paramlist...
????
}
result = string.Format(pFormat, ?paramlist?)
}

// now give the result back.....
return( result );
}

 
Note also that when you write something like

String.Format("{0}/{1}/{2}/{3}", 3,4,5,6);

Behind the scenes, the compiler actually translates that to:

object[] params = new object[4];
params[0] = 3;
params[1] = 4;
params[2] = 5;
params[3] = 6;
String.Format("{0}/{1}/{2}/{3}", params);
 
Back
Top