473,403 Members | 2,366 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

How To Duplicate A String

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

Dec 31 '06 #1
6 12906
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();
}
--
Tim Van Wassenhove <url:http://www.timvw.be/>
Dec 31 '06 #2
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
Dec 31 '06 #3
"sternr" <St****@gmail.comwrote in message
news:11*********************@k21g2000cwa.googlegro ups.com...
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...:-)
Dec 31 '06 #4
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

On Dec 31, 2:52 pm, "Mark Rae" <m...@markNOSPAMrae.comwrote:
"sternr" <Ste...@gmail.comwrote in messagenews:11*********************@k21g2000cwa.go oglegroups.com...
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...:-)
Dec 31 '06 #5
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/lib...b(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/archiv...07/348831.aspx)

Luke

"SÅ‚awek" <sp****************@o2.plwrote in message
news:en**********@node4.news.atman.pl...
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
Dec 31 '06 #6
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/lib...b(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/archiv...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
Jan 1 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

44
by: Xah Lee | last post by:
here's a large exercise that uses what we built before. suppose you have tens of thousands of files in various directories. Some of these files are identical, but you don't know which ones are...
8
by: Mark | last post by:
When my form goes to a new record, I have a procedure that copies the last record added to the form's underlying table into the form. The intent is that a series of new records may have the same...
9
by: vbportal | last post by:
Hi, I would like to add BitArrays to an ArrayList and then remove any duplicates - can someone please help me forward. I seem to have (at leaset ;-) )2 problems/lack of understanding (see test...
8
by: Iona | last post by:
Hi Allan, I'm using a nifty piece of code you put on here some time back to do a duplicate entry check as below. I'm using to check for duplicate names. However I am getting an error message on...
0
by: claus | last post by:
Hi, I am not a programmer but tries anyway to program a feature in a form where I am able to copy entries for the form and a subform. I have tried follow the guide here...
2
by: eazyone | last post by:
I can prevent a duplicate ID from being entered, but can't figure out how to go to that specific record. I keep getting an error message on: Me.Bookmark = rsc.Bookmark Its not opening and...
2
by: Harry Haller | last post by:
I want to duplicate the form data, edit it to remove some items (such as __EVENTTARGET, __EVENTVALIDATION, etc) and save it to a log. How can I make a duplicate (editable) copy of the Form...
4
by: mcca0081 | last post by:
hi - i'm trying to delete one of the duplicate records based on the most current date. here's the code for my access 2000 db. any help would be appreciated!!! - thank you kindly Sub...
1
by: xraive | last post by:
I have a problem with this. Currently I am trying Allen's code and i am not successful. Current Design Table1 (Main Form) TravelID (PK) ApprovedBY EntreredBy BudgetCode ExpenseCode
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.