DEV Community

Sunnat Qayumov
Sunnat Qayumov

Posted on

3 3 3 3 3

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
Enter fullscreen mode Exit fullscreen mode

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

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (1)

Collapse
 
asilbek_ibragimov_a0c27cc profile image
asilbek ibragimov

масса

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

AWS GenAI LIVE!

GenAI LIVE! is a dynamic live-streamed show exploring how AWS and our partners are helping organizations unlock real value with generative AI.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️