.NET Questions (CLOSED)

Questions and Answers on any aspect of .NET. Now closed.

This discussion group is now closed.

Have a question about .NET development? Try stackoverflow.com, a worldwide community of great developers asking and answering questions 24 hours a day.

The archives of .NET Questions contain years of Q&A. Even older .NET Questions are still online, too.

String composed of a character repeated x number of times

I am a Java newbie and I have a trivial question.

In Java, what function/method do we have for constructing composed of a single character repeated x number of times.

I am looking for the .NET framework equivalant of this String class constructor:

public String(char c, int numTimesToRepeat);

I looked at the java.lang.String class' constructors and couldn't find one that matched my needs.
Sathyaish Chakravarthy Send private email
Wednesday, April 04, 2007
 
 
So write a method to do it.
JJ Send private email
Wednesday, April 04, 2007
 
 
What does this question have to do with .NET?
Art Metz Send private email
Wednesday, April 04, 2007
 
 
If you find one let me know. I spent quite a bit of time looking for a Java equivalent and never found one. But maybe it was right under my nose the whole time. Who knows?

But this is just one of many reasons why I like C# much better than Java.
anony mouse
Wednesday, April 04, 2007
 
 
Am I missing something? Why would anyone spend any significant time (more than 60 seconds) looking for such a method when you could write one in less than 60 seconds?
JJ Send private email
Wednesday, April 04, 2007
 
 
JJ: If the number of times to repeat was sufficiently large a simple "for" loop of concatenation would be very inefficient.  Allocating from memory a raw set of bytes and using pointers to set the value would be much quicker, but getting to that level to my knowledge is a little tricky in C# and probably not possible in VB.NET and Java.

There may be other tricks to efficiently do this in Java and VB.NET, but once one ponders the problem a little while it's natural to search for a de-facto implementation of the method done at a low level by someone who really knows where the bits are and what they're doing.
Jason Pettys Send private email
Wednesday, April 04, 2007
 
 
By concatenations, I assume you mean String concatenations?

Wouldn't creating a char array of size numTimesToRepeat and filling that avoid any string concatenations?

private static String foo(char c, int n){
  char[] a = new char[n];
  for(int i=0; i<n; i++) a[i]=c;
  return new String(a);
}

Is there a more efficient way?
JJ Send private email
Wednesday, April 04, 2007
 
 
JJ: While your code is fast, the String object still has to copy your char array into its own buffer.  Any MakeString(ch, count) function you write won't be as fast as the optimum constructor because of string immutability (it has to make a copy of whatever you hand it).
But the OP didn't say anything about performance, and the whole discussion is arguable off-topic anyway.
bmm6o Send private email
Wednesday, April 04, 2007
 
 
"Am I missing something? Why would anyone spend any significant time (more than 60 seconds) looking for such a method when you could write one in less than 60 seconds? "

Because if there is one already available in the framework it would be silly to write your own. The Daily WTF is chock full of examples of people writing basic functions that are already available simply because they didn't take the time to ask. When you have 10 programmers working on a common codebase it pays to research and decide on a stanrdad way of doing this kind of thing. Otherwise, you end up with 10 different implementations of something that was readily available.

So although the question may have gotten a less hostile response in the Design of Software forum, it is still a legitimate question.
anony mouse
Wednesday, April 04, 2007
 
 
Thanks to everyone who replied.

The short answer I've gotten is: there is none.

I did write my own function before posting this question. I was still looking for an answer to this question because:

a. That's the way I am;

b. I believe the authors of frameworks are more efficient than I am, even if my for loop would've had no additional cost to it and would've been done in constant time.

c. If I'd learnt about such a function existing, it would've made be a tad bit better informed about the framework. In today's programming world with all languages having almost similar syntax, the steepest learning curve is getting familiar with the different frameworks. Vocabulary is more of a challenge than grammar if you've already programmed any language of the same family (functional, procedural, OO) before.

Thanks again.
Sathyaish Chakravarthy Send private email
Thursday, April 05, 2007
 
 
string x = new System.Text.StringBuilder().Append('x',42).ToString();

Kinda wordy but it works very efficiently.
JSmith Send private email
Thursday, April 05, 2007
 
 
In .Net 2.0 the String class has the constructor:

public String (
    char c,
    int count
)

Which seems to be what you are looking for.
Arethuza
Saturday, April 07, 2007
 
 
This will also work:

string bar = string.Empty.PadLeft(20, 'x');

Obviously you don't have to start with String.Empty, nor do you have to use PadLeft, but this will give you a series of 20 x's in a new string.

Monday, April 09, 2007
 
 
"In .Net 2.0 the String class has the constructor"

Wouldn't it be nice if Constants could be WORM
(Write Once, Read Many) at run time?

Then I could say

  Const Spaces_256 = new String(" ", 256)

or

  Const Twenty_Questions _
        = {"Is it bigger than a breadbox?", _
          "Is it animal, vegetable, or mineral?", _
          .....

Of course, I could change Const to Dim but ....
Wes Groleau Send private email
Wednesday, April 18, 2007
 
 

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics
 
Powered by FogBugz