C# da StringBuilder
Published: 2024-04-14 21:41:44

C# dasturlash tilida string immutable (o’zgartirib bo’lmaydigan) tip.  Ya’ni string bir marta yaratilsa uni o’zgartirib bo’lmaydi. Misol uchun, str = “Hello World” degan stringni olaylik, u heap xotiradan joy egallaydi. Agar uni str = “Hello Wrold from DOTNET” ga o’zgartiradigan bo’lsak, oldingi “Hello World” degan string o’zgartirilmasdanm butunlay boshqa yangi “Hello Wrold from DOTNET” degan string yaratiladi. Agar string bilan bog’liq o’zgartirish, o’chirish, yangi string qo’shish ishlari juda ko’p bo’ladigan bo’lsa bu dasturni ishlashini qiyinlashtiriadi.


![ram1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r3j2hn4duzm450m269mc.jpg)

Yuqorida aytilgan muammoni yechish uchun, C# da **StringBuilder** class mavjud, u System.Text degan namespacega tegishli. **StringBuilder** yangi string object yaratmasdan xotirani dinamik tarzda o’zgartirgan xolda string ma’lumotni o’zgartirish (modifiy qilish) imkonini beradi. 


![ram2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iax90z695xl3szhb0cja.jpg)

## StringBuilder obyektini yaratish.

StringBuilder obyektini yaratish uchun **new** kalit so’zidan foydalanamiz.

```csharp
using System.Text; // StringBuilder classi ishlashi uchun System.Text degan namespaceni
//using qilib chaqirib qo'yamiz
            
StringBuilder sb = new StringBuilder(); // StringBuilder obyektini yaratib olamiz
//yoki obyektni yaratish jarayonida unga quiydagicha boshlang'ich qiymatni berib
//ketsak ham bo'ladi.
StringBuilder sb = new StringBuilder("Hello World!"); 
```

StringBuilder orqali haratilgan objectning har bitta elementini loop orqali ko’rib chiqsak bo’ladi.

```csharp
StringBuilder sb = new StringBuilder("Hello World!");

for(int i = 0; i < sb.Length; i++)
    Console.Write(sb[i]); // output: Hello World!
```

## !Eslatma.

StringBuilder bu string type emas. Agar string type kerak bo’lsa, ToString() metodi orqali stringga o’tkazib olsak bo’ladi.

```csharp
StringBuilder sb = new StringBuilder("Hello World!");

var greet = sb.ToString(); //returns "Hello World!"
```

## StringBuilder objectiga string qo’shish.

Buning uchun **Append()** (StringBuilder objetning oxiriga stirng qo’shadi) yoki **AppendLine()** (StringBuilder objetning oxiriga stirngni yangi qator bilan birga qo’shadi qo’shadi) methodlaridan fodalanamiz.

```csharp
StringBuilder sb = new StringBuilder();
sb.Append("Hello ");
sb.AppendLine("World!");
sb.AppendLine("Hello C#");
Console.WriteLine(sb);

//Output:
//Hello World!
//Hello C#.
```

## StringBuilder objectiga stringni formatlab qo’shish.

```csharp
StringBuilder sbAmout = new StringBuilder("Your total amount is ");
sbAmout.AppendFormat("{0:C} ", 25);

Console.WriteLine(sbAmout);//output: Your total amount is $ 25.00
```

## Insert()

Bu method StringBuilder objetining istalgan joyiga string qo’shish uchun ishlatiladi, va buning uchun qaysi indexdan boshlab string joylamoqchi ekanlgimizni aytish kerak bo’ladi.

```csharp
StringBuilder sb = new StringBuilder("Hello World!");
sb.Insert(5," C#"); 

Console.WriteLine(sb); //output: Hello C# World!
```

## Remove()

Bu method orqali StringBuilder objectining qaysi indexidan boshlan necha character o’chirilish aytiladi.

```csharp
StringBuilder sb = new StringBuilder("Hello World!",50);
sb.Remove(6, 7); // 6 - indexdan boshlab, 7 ta characterni o'chir deyilaypti

Console.WriteLine(sb); //output: Hello
```

## Replace()

 

```csharp
StringBuilder sb = new StringBuilder("Hello World!");
sb.Replace("World", "C#");

Console.WriteLine(sb);//output: Hello C#!
```

`sb.Replace(str1, str2)` bu yerda str1 biz o’zgartirmoqchi bo’lgan qisim string (substing), str2 esa srt1 ni o’rniga yozilishi kerak bo’lgan string. str1 StringBuilde objectning ichida necha marta uchras hammasini str2 bilan almashtirib qo’yadi.