String Operations

kudratswe

Kudrat

Posted on April 16, 2024

String Operations

is String literal is mutable or immutable?

Agar biz quyidagi kod misolida oladigan bo'lsak

string code  = "Hello World!";
Enter fullscreen mode Exit fullscreen mode

Bu yerda "Hello World!" string literal deb ataladi.String literallar kompilyatsiya vaqtida yaratiladi va kodni bir qismi sifatida ishlatiladi.Ya'ni kod xotiraga qachon yuklansa,o'sha payt yuklanadi.Va string literallar immutable ya'ni qiymatini o'zgartirib bo'lmaydi
Code esa bu yerda string variable va uning qiymatini o'zgartirish mumkin ya'ni mutable.

Stack and Heap (Briefly)

Dastur davomida 2 turdagi xotira yaratiladi.Ular:

Stack va Heap
Stack xotirasida dastur boshlanishida operatsion sistema qancha xotira kerakligini biladi va buni avtomatik tarzda belgilab beradi.Va uning hajmi o'zgarmaydi.
Heap esa dynamic xotira qismi ya'ni dastur ishga tushgandan so'ng qo'shimcha xotiraga ehtiyoj sezilsa,Heap qismidan istalgancha so'rab olaveradi.
Stringlar C# da reference type hisoblanadi va xotiraning Heap qismida saqlandi.

int son = 15;
Enter fullscreen mode Exit fullscreen mode

Bu yerda 15 value type va u Stack qismida saqlanadi.

Image description

Agarda biz

string code  = "Hello World!";
code = "Hi";
Enter fullscreen mode Exit fullscreen mode

shunaqa amal bajarmoqchi bo'lsak,literalning qiymati o'zgarmaydi,shunchaki heapdan yangi adress ajratiladi va eski adres OS ga qaytarib beriladi.

Image description

Stringni har bir elementiga birma bir indeksiga murojaat qilgan holda chop etsa bo'ladi

string code  = "Hello World!";
Console.WriteLine(name[0]);  //output : H
Console.WriteLine(name[1]);  //output : e
Enter fullscreen mode Exit fullscreen mode

String Concatenation

var greet = "Salom" + " " + code;
Console.WriteLine(greet);
Enter fullscreen mode Exit fullscreen mode

or

greet += "!";
Console.WriteLine(greet);
Enter fullscreen mode Exit fullscreen mode

stringlarni bir biriga qo'shish maqsadida ishlatiladi.

String Interpolation

String Interpolation-string ichiga ma'lumotlarni inject qilish ya'ni joylash.

var greet2 = $" Hello, {code}";
Console WriteLine(greet2);
Enter fullscreen mode Exit fullscreen mode

or

Console.WriteLine($"Hello,{name}");
Enter fullscreen mode Exit fullscreen mode

//Formatting

var butunSon = 25;
Console.WriteLine($"Butun son : {butunSon :10}");

Enter fullscreen mode Exit fullscreen mode

Bu yerda 10 yozganimiz uchun natijani o'ng tomondan 10 ta belgi rostlab chop etadi.-10 yozadigan bo'lsak chap tomondan rostlab natijani chiqaradi.
Bundan tashqari

Console.WriteLine($"Butun son : {butunSon :10:F3}");
Console.WriteLine($"Butun son : {butunSon :10:C3}");
Console.WriteLine($"Butun son : {butunSon :10:P3}");
Enter fullscreen mode Exit fullscreen mode

F-->Floating point,C-->Currency,P-->Percentage qiymatlarda chop etadi.
//Composite Formatting
String avval yozib keyin qiymatlari beriladi:

var ajoyib =3.14;
//var tekst = string.Format("Bu {0:C3} tekst", ajoyib);
Console.WriteLine("Bu {0:C3} tekst", ajoyib);
//output : Bu $3.140 tekst
Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
kudratswe
Kudrat

Posted on April 16, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

What was your win this week?
weeklyretro What was your win this week?

November 29, 2024

Where GitOps Meets ClickOps
devops Where GitOps Meets ClickOps

November 29, 2024

How to Use KitOps with MLflow
beginners How to Use KitOps with MLflow

November 29, 2024

Modern C++ for LeetCode 🧑‍💻🚀
leetcode Modern C++ for LeetCode 🧑‍💻🚀

November 29, 2024