DEV Community

asilbek ibragimov
asilbek ibragimov

Posted on

String

Is string literal mutable or immutable ?
Is string variable mutable or immutable ?

String literal - deb " " ichida constanta sifatida yozilgan stringga aytiladi.

String literal immutable - degani bu qoymatni o'zgaritirib bo' lmaydi.**

var name = "Hello World";
name = "hello world";
var son = 99;
son = 98;
Console.WriteLine(name);
Console.WriteLine(son);
Enter fullscreen mode Exit fullscreen mode

Value typelar - stactkda saqlanadi
Value type deganda, ma'Lumotlar tog'ridan- to'g'ri o'zgaruvchida saqalanadi. Bunda o'zgaruvchining qiymati o'zi bilan birga sagalanadi.
Value typelar stack xotirasida saqlanadi.
Value typega quydagilar kiradi.
Oddiy turlar (int, float, double, char bool va hokazo)
struct, enum.

  int a = 5;
  int b = a;

  b = 10;

  Console.WriteLine(a);
  Console.WriteLine(b);
Enter fullscreen mode Exit fullscreen mode

String metodlari
TrimStart() - function tekstni oldidagi joylarni olib tashlaydi.
TrimEnd() - function tekstni oxiridagi joylarni olib tashlaydi
Trim() - fucntion oldi va oxiridagi joylarni tengdaniga olib tashlaydi.

Bunga misol:

string greeting2 = "Hello"
Console.WriteLine(greeting2);
string trimmedGreeting = greeting2.TrimStart();
Console.WriteLine(trimmedGreeting);
trimmedGreeting = greeting2.TrimEnd();
Console.WriteLine(trimmedGreeting);
trimmedGreeting = (greeting2.Trim);
Console.WriteLine(trimmedGreeting);
Enter fullscreen mode Exit fullscreen mode

Replace()
Function tekst qiymatini o'rniga yangi qiymatga o'zgartirrish imkoniyatini beradi.

 string sayHello= "Hello World";
 Console. WriteLine(sayHello);
 sayHello = sayHello.Replace("Hello", "Greetings");
 Console.WriteLine(sayHello);
Enter fullscreen mode Exit fullscreen mode

ToUpper()
Function xamma harflarni kotta qilib beradi.
ToLower()
Fucntion xamma harflarni kichik qilib beradi.

 Console. WriteLine(sayHello.ToUpper());
 Console. WriteLine(sayHello.ToLower());
Enter fullscreen mode Exit fullscreen mode

Split()
Stringni substringlarga ajratadi va string array qaytaradi.

 var originalString = "olma anor nok";
 var mevalar = originalString.Split(' ');
 Console.WriteLine(mevalar[0]);
 Console.WriteLine(mevalar[1]);
 Console.WriteLine(mevalar[2]);
 output:
 olma
 anor
 nok
Enter fullscreen mode Exit fullscreen mode

Format()
Stringni formatlash uchun ishlatiladi.

  var testString = string.Format("Hello {0}", 123123);
  Console.WriteLine("Hello {0:C}", 123123);
  output:Hello 123123
Enter fullscreen mode Exit fullscreen mode

String concatenation - stringlarni bir biriga qo'shish
var greeting = "Hello" + " " + name + " ":
Console.WriteLine(greeting);

Agar space holder ichida o'zgaruvchidan keyin, 10 soni yoziladigan bo lsa bizga consoledan 10 ta joy ajratib beradi.
Agar -10 yoziladigan bo' lsa chap tarfdan rostlab chop etadi.

 var butunSon = 2;
 Console.WriteLine($"Kiritilgan butunSon: {butunSon, 10:D4}" ):
 Console. WriteLine($"Kiritilgan butunSon: butunSon, -10: 04}");
Enter fullscreen mode Exit fullscreen mode

Escape character && verbatim string
Escape character maxsus belgilarni chop etishda backslashda foydalanishimiz kerak - |

Misol uchun:

    var specialString = "U judayam \"zor\" bola";
    Console.WriteLine(specialString):
    Console.WriteLine("olma\n anor\n nok");
    Console.WriteLine("olma \t anor \t nok");
    Console.WriteLine("olma \b anor");
    Console.WriteLine("olma\\anor\\nok");
    Console.WriteLine("C:\\|Windows||System32\\calc.exe");
    Console.WriteLine(@"C:\Windows\System32\calc.exe");
    Console.WriteLine("C: \n Windows \n System32 \n calc.exe");
    Console.WriteLine(@"C: \n Windows \n Systen32 \n calc.exe");
Enter fullscreen mode Exit fullscreen mode

Top comments (2)

Collapse
 
sunnat_qayumov profile image
Sunnat Qayumov

Qattu

Collapse
 
asilbek_ibragimov_a0c27cc profile image
asilbek ibragimov

йее