DEV Community

Sunnat Qayumov
Sunnat Qayumov

Posted on

C# - String methods

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

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

Quyidagi hollarda **Split()** ko'p ishlatiladi:

var sum = Console.ReadLine()?
    .Split(' ', StringSplitOptions.RemoveEmptyEntries)
    .Select(int.Parse)
    .Aggregate((x, y) => x + y);
Console.WriteLine(sum);
Enter fullscreen mode Exit fullscreen mode

Istagancha sonni string ko'rinishida kiritadigan bo'lsak,uni int tipiga o'tkazib,bo'sh probellarni olib tashlab yigindisini hisoblaydi.
Bu yerda RemoveEmptyEntries funksiyasi bo'sh probellarni olib tashlash uchun ishlatilsa,TrimEmptyEntries begilarning ikkala tomonidagi ya'ni oldi va ortidagi probellarni olib tashlaydi.

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

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

Contains()
Stringning berilgan substringdan tashkil topgan yoki yo'qligini tekshiradi.

string songLyrics = "You say goodbye, and I say hello";
Console.WriteLine(songLyrics.Contains("goodbye"));
Console.WriteLine(songLyrics.Contains("say"));
Console.WriteLine(songLyrics.Contains("greetings"));
Enter fullscreen mode Exit fullscreen mode

Trim()
Stringning oldi yoki ortidagi barcha bo'sh kataklarni o'chirish uchun ishlatiladi.

var stringWithSpaces = "  hello world     \n";
Console.WriteLine(stringWithSpaces);
Console.WriteLine(stringWithSpaces.Trim());
Console.WriteLine(stringWithSpaces.TrimEnd());
Console.WriteLine(stringWithSpaces.TrimStart());
Enter fullscreen mode Exit fullscreen mode

EndWith()
String berilgan string bilan tugash yoki tugamasligini tekshiradi.

Console.WriteLine(songLyrics.EndsWith("hello"));
Console.WriteLine(songLyrics.EndsWith("goodbye"));
Enter fullscreen mode Exit fullscreen mode

StartWith()
String berilgan string bilan boshlanishini tekshiradi.

Console.WriteLine(songLyrics.StartsWith("You"));
Console.WriteLine(songLyrics.StartsWith("goodbye"));
Enter fullscreen mode Exit fullscreen mode

ToUpper()
Katta harfga aylantirib qo'yadi.

Console.WriteLine("ToUpper()");
Console.WriteLine(longString.ToUpper());
Enter fullscreen mode Exit fullscreen mode

ToLower()
Kichik harfga o'zgartirib qo'yadi.

Console.WriteLine("ToLower()");
Console.WriteLine(longString.ToLower());
Enter fullscreen mode Exit fullscreen mode

Top comments (3)

Collapse
 
muslima_abdurahmonova_889 profile image
Muslima Abdurahmonova

Gʻazini bos gap yoʻ

Collapse
 
asilbek_ibragimov_a0c27cc profile image
asilbek ibragimov

гап жок йорворейшн