DEV Community

Sunnat Qayumov
Sunnat Qayumov

Posted on

2 3 2 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

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

Hot sauce if you're wrong - web dev trivia for staff engineers

Hot sauce if you're wrong · web dev trivia for staff engineers (Chris vs Jeremy, Leet Heat S1.E4)

  • Shipping Fast: Test your knowledge of deployment strategies and techniques
  • Authentication: Prove you know your OAuth from your JWT
  • CSS: Demonstrate your styling expertise under pressure
  • Acronyms: Decode the alphabet soup of web development
  • Accessibility: Show your commitment to building for everyone

Contestants must answer rapid-fire questions across the full stack of modern web development. Get it right, earn points. Get it wrong? The spice level goes up!

Watch Video 🌶️🔥

Top comments (3)

Collapse
 
muslima_abdurahmonova_889 profile image
Muslima Abdurahmonova

Gʻazini bos gap yoʻ

Collapse
 
asilbek_ibragimov_a0c27cc profile image
asilbek ibragimov

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

AWS Q Developer image

Your AI Code Assistant

Generate and update README files, create data-flow diagrams, and keep your project fully documented. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

👋 Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay