views:

173

answers:

13

I am looking for a way to generate N pieces of question marks joined with comma.

string element="?";
string sep=",";
int n=4;
// code to run and create ?,?,?,?
  • EDIT 1

I am looking in a simple way. Probably using 1-2 line of code. In c++ there are array fill() and joins.

  • EDIT 2

I need this for Compact Framework

+1  A: 

In C# I would do:

StringBuilder sb = new StringBuilder();
string element="?";
string sep=",";
int n=4;

for (int x = 0; x < n; x++)
{
    sb.Append(element);
    sb.Append(sep);
}

if (sb.Length > 0)
{
    // remove the final separator
    sb.Length -= sep.Length;
}

Console.WriteLine(sb.ToString());
Michael Todd
What if `sep` isn't a single character?
Mark E
Good point. Updated.
Michael Todd
This is the way I have alway done it as well
Gordon Tucker
A: 

Not concise but it'll do the trick..

StringBuilder sb = new StringBuilder();
sb.Append(element);
for(int i = 1; i < n; i++){
   sb.Append(sep);
   sb.Append(element);
}
string output = sb.toString();
Mark E
A: 
StringBuilder sb = new StringBuilder();
for(int i=0;i<n;i++)
  sb.Append((i != n-1) ? "?," : "?");
string result = sb.ToString();
sashaeve
A: 

One line

String s = "".PadLeft(n-1, 'X').Replace("X", element+sep) + element;
f3lix
I guess this was downvoted because it fails for n==0?
finnw
A: 
string element = "?";
string sep = ",";
int n = 4;

string[] array = new string[n];

for (int i = 0; i < array.Length; i++) {
    array[i] = element;
}

string result = String.Join(sep, array);
Rafa Castaneda
A: 
        string output = "?";
        int n = 4;
        while (n > 1)
        { output += "," + "?"; n--; }
Asad Butt
Doesn't this increase exponentially? You end up with "?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?" i.e. 16 ?'s not the required 4!
Grhm
and how is that ?
Asad Butt
try it out and you will see
Pentium10
Thanks mate, have made the correction, It was not exponential. The string was repeated!
Asad Butt
+2  A: 
static string BuildSeparatedString(string element, string sep, int count)
{
  StringBuilder sb = new StringBuilder();

  for (int i = 1; i <= count; i++)
  {
    sb.Append(element);

    if (i != count)
      sb.Append(sep);
  }
  return sb.ToString();
}

It's not fancy but neither is it a cryptic one liner. Anyone reading or maintaining the code should very quickly understand it.

By some quick testing this also runs nearly twice as fast as the two most popular one liners.

Kevin Gale
+3  A: 
var result = Enumerable.Repeat(element, n).DefaultIfEmpty("").Aggregate((s1, s2) => s1 + sep + s2);
Mark Seemann
Although this is tricky its way too complicated..
Stan R.
what would be the long path for Enumerable eg : System.Collections. Generic.Enumerable ..... ? I can't find it. I am CompactFramework.
Pentium10
It's System.Linq.Enumerable in System.Core, but I don't know if it's in CF as well.
Mark Seemann
I think all these one liners are way too clever. Having one line do several things at once doesn't make it faster or more efficient. It just makes the code harder to understand. The simple string builder for loop is probably faster and it is certainly eaiser to understand.
Kevin Gale
According to the documentation, it's supported on CF 3.5: http://msdn.microsoft.com/en-us/library/bb348899.aspx
Mark Seemann
It turns out it doesn't help me, as I am not on Compact Framework 3.5 Anyway thanks for the effort, I will use other one.
Pentium10
+1  A: 
static class Extensions
{
     public static string Times(this string s, int count)
     {
          StringBuilder sb = new StringBuilder(count * s.Length);
          for (int i = 0; i < count; i++)
          {
              sb.Append(s);
          }
          return sb.ToString();
     }
}

Usage:

Console.WriteLine("?,".Times(5).Trim(','));
jvenema
+9  A: 

Use the new Enumerable.Repeat method in conjunction with String.Join:

String.Join(sep, Enumerable.Repeat(element, n).ToArray());
Jon Benedicto
+1, far more readable than the accepted answer
Mark E
+1 forgot about repeat.
Stan R.
what would be the long path for Enumerable eg : System.Collections. Generic.Enumerable ..... ? I can't find it. I am CompactFramework.
Pentium10
+1 Damn that's elegant.
Pwninstein
@Pentium10 - System.Linq.Enumerable.Repeat - not sure if linq is in CF
David B
It turns out it doesn't help me, as I am not on Compact Framework 3.5 Anyway thanks for the effort, I will use other one.
Pentium10
A: 

Here is a one liner as well :)

 int n = 10;
 string element = "?";
 string sep = ",";

 string result = String.Join(sep, Enumerable.Range(0, n).Select(x => element).ToArray());
Stan R.
what would be the long path for Enumerable eg : System.Collections. Generic.Enumerable ..... ? I can't find it. I am CompactFramework.
Pentium10
It turns out it doesn't help me, as I am not on Compact Framework 3.5 Anyway thanks for the effort, I will use other one.
Pentium10
no problem, you have many choices to choose from.
Stan R.
A: 
string result = String.Join(sep,Enumerable.Repeat(element, n).ToArray());
Gregoire
It turns out it doesn't help me, as I am not on Compact Framework 3.5 Anyway thanks for the effort, I will use other one.
Pentium10
A: 

Here's another one-line expression to do what you want (with LINQ):

string.join(sep, (from x in Enumerable.Range(1, n) select element).ToArray())
Gabe
It turns out it doesn't help me, as I am not on Compact Framework 3.5 Anyway thanks for the effort, I will use other one.
Pentium10