Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C#

วิธีสร้าง String ซ้ำ N ครั้งใน C#

มีหลายวิธีในการสร้าง String ที่ซ้ำกัน N ครั้งใน C#

1. ใช้ String Constructor (สำหรับ Char เดียว)

ใช้ new string(charToRepeat, count) เพื่อทำซ้ำ Character เดียว

using System;

namespace DemoApplication {
    public class Program {
        static void Main(string[] args) {
            string myString = "Hi";
            Console.WriteLine($"String: {myString}");
            
            char charToRepeat = '!';
            Console.WriteLine($"Character to repeat: {charToRepeat}");
            
            string repeatedString = new string(charToRepeat, 5);
            Console.WriteLine($"Repeated String: {myString}{repeatedString}");
            Console.ReadLine();
        }
    }
}

Output

String: Hi
Character to repeat: !
Repeated String: Hi!!!!!

2. ใช้ string.Concat และ Enumerable.Repeat

ใช้ string.Concat(Enumerable.Repeat(charToRepeat, count)) สำหรับ Char หรือ String

using System;
using System.Linq;

namespace DemoApplication {
    public class Program {
        static void Main(string[] args) {
            string myString = "Hi";
            char charToRepeat = '!';
            
            var repeatedString = string.Concat(Enumerable.Repeat(charToRepeat, 5));
            Console.WriteLine($"Repeated String: {myString}{repeatedString}");
            Console.ReadLine();
        }
    }
}

3. ใช้ StringBuilder (แนะนำสำหรับ Loop/Performance)

using System;
using System.Text;

namespace DemoApplication {
    public class Program {
        static void Main(string[] args) {
            string myString = "Hi";
            string stringToRepeat = "!";
            
            StringBuilder builder = new StringBuilder(stringToRepeat.Length * 5);
            for (int i = 0; i < 5; i++) {
                builder.Append(stringToRepeat);
            }
            string repeatedString = builder.ToString();
            
            Console.WriteLine($"Repeated String: {myString}{repeatedString}");
            Console.ReadLine();
        }
    }
}

4. ใช้ String.Join (สำหรับ Array/List)

string repeated = string.Join("", Enumerable.Repeat("Hi", 5));
// ผลลัพธ์: HiHiHiHiHi

5. Extension Method สำหรับความสะดวก (C# 10+)

public static class StringExtensions {
    public static string Repeat(this string str, int count) {
        if (count <= 0) return string.Empty;
        if (str.Length == 1) return new string(str[0], count);
        
        var builder = new StringBuilder(str.Length * count);
        for (int i = 0; i < count; i++) builder.Append(str);
        return builder.ToString();
    }
}

// การใช้งาน
string result = "Hi".Repeat(5); // "HiHiHiHiHi"
char c = '!';
string result2 = c.Repeat(5); // "!!!!!" (ต้องมี overload สำหรับ char)

Benchmark Comparison

MethodCharStringPerformanceNote
new string(c, n)✓✗เร็วที่สุดBuilt-in, Optimized
StringBuilder✓✓เร็วมากเหมาะ Loop/Concat หลายครั้ง
Enumerable.Repeat + Concat✓✓ปานกลางReadable, LINQ Style
String.Join✗✓ปานกลางเหมาะ Array/List

สรุป

  • Char เดียว → new string(c, n) (เร็วที่สุด)
  • String / General → StringBuilder ใน Loop
  • One-liner / LINQ Style → string.Concat(Enumerable.Repeat(...))
  • Array/List ของ String → String.Join