How To Duplicate A String

  • Thread starter Thread starter sternr
  • Start date Start date
S

sternr

Hey,
Is there a way to duplicate a certain string multiple times?
I.E: I've got the following string: "<0>" and would like to create:
"<0><0><0><0>".
Is there a CLR API for this behavior or should I create this function
myself?
Thanks

--sternr
 
sternr schreef:
Hey,
Is there a way to duplicate a certain string multiple times?
I.E: I've got the following string: "<0>" and would like to create:
"<0><0><0><0>".
Is there a CLR API for this behavior or should I create this function
myself?

(untested)

public static string StringCopier(string input, int numberOfCopies) {
if (input == null) { throw new ArgumentNotNullException(); }

StringBuilder output = new StringBuilder(input.length * numberOfCopies);
for (int i = 0; i < numberOfCopies; ++i) {
output.Append(input);
}

return output.ToString();
}
 
sternr napisał(a):
Hey,
Is there a way to duplicate a certain string multiple times?
I.E: I've got the following string: "<0>" and would like to create:
"<0><0><0><0>".
Is there a CLR API for this behavior or should I create this function
myself?
Thanks

--sternr

private string Duplicate(string partToDuplicate, int howManyTimes)
{
string result="";
for (int =0; i < howManyTimes; i++)
result +=partToDuplicate;
return result;
}

have a nice day ;)
Slawomir
 
Is there a way to duplicate a certain string multiple times?
I.E: I've got the following string: "<0>" and would like to create:
"<0><0><0><0>".
Is there a CLR API for this behavior or should I create this function
myself?

You can duplicate a single character n number of times:

string strTest = new String(char, n);

but not an actual string.

However, as others have pointed it, a custom function to do what you want is
not particularly challenging...:-)
 
Wow, thank you all for your answers!
But as Mark said - writing my own duplication function is reall a
child's play -
I've been looking for a CLR function like the one Mark gave (thanks
man!)
Nevertheless - thank you all for your help!
Thread Closed

--sternr
 
Luke Smith napisał(a):
You should use a StringBuilder (Tim's approach) instead of concatenating
strings as there is a performance benefit in this situation
(http://msdn2.microsoft.com/en-gb/library/za88c8yb(vs.80).aspx).

Concatenation can be faster when you know how many concatenations are
required (Are StringBuilders always faster than concatenation?:
http://weblogs.asp.net/bleroy/archive/2005/01/07/348831.aspx)

Luke

It depends ... using concatenation is much more intuitive and easy to
understand. However I understand and accept that using stringbuiler is
much more efficient when dealing with big number of huge string.

best,
Slawomir
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top