C# में किसी कैरेक्टर या स्ट्रिंग को N बार दोहराने के कई तरीके हैं।
विधि 1: string कंस्ट्रक्टर (केवल सिंगल कैरेक्टर के लिए)
using System;
namespace DemoApplication
{
public class Program
{
static void Main()
{
string myString = "Hi";
char charToRepeat = '!';
// string कंस्ट्रक्टर: new string(char, count)
string repeatedString = new string(charToRepeat, 5);
Console.WriteLine($"String: {myString}");
Console.WriteLine($"Character to repeat: {charToRepeat}");
Console.WriteLine($"Repeated String: {myString}{repeatedString}");
Console.ReadLine();
}
}
}
आउटपुट
String: Hi Character to repeat: ! Repeated String: Hi!!!!!
सीमा: यह केवल char के साथ काम करता है, पूरे स्ट्रिंग के साथ नहीं।
विधि 2: string.Concat + Enumerable.Repeat
कैरेक्टर या स्ट्रिंग दोनों के लिए काम करता है।
using System;
using System.Linq;
namespace DemoApplication
{
public class Program
{
static void Main()
{
string myString = "Hi";
char charToRepeat = '!';
// Enumerable.Repeat एलिमेंट को N बार दोहराता है, Concat उन्हें जोड़ता है
var repeatedString = string.Concat(Enumerable.Repeat(charToRepeat, 5));
Console.WriteLine($"Repeated String: {myString}{repeatedString}");
// स्ट्रिंग के साथ भी काम करता है
string word = "Ha";
var repeatedWord = string.Concat(Enumerable.Repeat(word, 3));
Console.WriteLine($"Word repeated: {repeatedWord}"); // HaHaHa
Console.ReadLine();
}
}
}
आउटपुट
Repeated String: Hi!!!!! Word repeated: HaHaHa
विधि 3: StringBuilder (उच्च प्रदर्शन, लूप के लिए)
using System;
using System.Text;
namespace DemoApplication
{
public class Program
{
static void Main()
{
string myString = "Hi";
string stringToRepeat = "!";
int count = 5;
// क्षमता पूर्व-आवंटित करें (प्रदर्शन के लिए)
StringBuilder builder = new StringBuilder(stringToRepeat.Length * count);
for (int i = 0; i < count; i++)
{
builder.Append(stringToRepeat);
}
string repeatedString = builder.ToString();
Console.WriteLine($"Repeated String: {myString}{repeatedString}");
Console.ReadLine();
}
}
}
आउटपुट
Repeated String: Hi!!!!!
विधि 4: string.PadLeft/PadRight (ट्रिक)
string repeated = "x".PadRight(5, 'x'); // "xxxxx"
string repeated2 = "x".PadLeft(5, 'x'); // "xxxxx"
// स्ट्रिंग के लिए (कम सीधा)
string word = "ab";
string repeatedWord = string.Concat(Enumerable.Repeat(word, 3)); // बेहतर: ऊपर की विधि 2
विधि 5: C# 11+ - String.Create (उच्च प्रदर्शन)
// .NET 7 / C# 11+
string Repeat(string value, int count)
{
if (count <= 0) return string.Empty;
if (count == 1) return value;
return string.Create(value.Length * count, (value, count),
(span, state) =>
{
for (int i = 0; i < state.count; i++)
{
state.value.AsSpan().CopyTo(span.Slice(i * state.value.Length));
}
});
}
// उपयोग
var result = Repeat("Hi", 3); // "HiHiHi"
प्रदर्शन तुलना
| विधि | उपयोग मामला | प्रदर्शन | आवंटन |
|---|---|---|---|
new string(char, n) | सिंगल कैरेक्टर | सबसे तेज़ | 1 आवंटन |
string.Concat(Enumerable.Repeat) | कैरेक्टर या स्ट्रिंग | अच्छा | कई आवंटन |
StringBuilder | स्ट्रिंग, लूप में | उत्कृष्ट (प्रीअलोकेटेड) | 1-2 आवंटन |
string.Create (.NET 7+) | स्ट्रिंग, उच्च प्रदर्शन | सबसे तेज़ (स्ट्रिंग के लिए) | 1 आवंटन |
रीयूजेबल एक्सटेंशन मेथड्स
public static class StringExtensions
{
// कैरेक्टर दोहराएं
public static string Repeat(this char c, int count)
{
if (count <= 0) return string.Empty;
return new string(c, count);
}
// स्ट्रिंग दोहराएं
public static string Repeat(this string s, int count)
{
if (string.IsNullOrEmpty(s) || count <= 0) return string.Empty;
if (count == 1) return s;
// StringBuilder दृष्टिकोण
var builder = new StringBuilder(s.Length * count);
for (int i = 0; i < count; i++)
builder.Append(s);
return builder.ToString();
}
// LINQ दृष्टिकोण (संक्षिप्त)
public static string RepeatLinq(this string s, int count)
{
return string.Concat(Enumerable.Repeat(s, count));
}
}
// उपयोग
'!'.Repeat(5) // "!!!!!"
"Hi".Repeat(3) // "HiHiHi"
"Ha".RepeatLinq(3) // "HaHaHa"
सारांश
- सिंगल कैरेक्टर:
new string(c, n)- सबसे सरल और तेज़ - स्ट्रिंग (वन-लाइनर):
string.Concat(Enumerable.Repeat(s, n)) - स्ट्रिंग (उच्च प्रदर्शन):
StringBuilderयाstring.Create(.NET 7+) - लूप में:
StringBuilderका उपयोग करें बार-बार कन्कैट से बचने के लिए