DEV Community

Kudrat
Kudrat

Posted on

String Methods in C#

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.

Substring()

Stringning substringini berilgan indeksdan boshlab qaytarib beradi.

var longString = "This is a very long string";
Console.WriteLine(longString.Substring(5));
Console.WriteLine(longString.Substring(5, 10));
Enter fullscreen mode Exit fullscreen mode

Replace()

Belgilangan eski belgini yangisiga o'zgartirish uchun ishlatiladi
Bu metod ham xuddi Substring metodi kabi yangi string qaytaradi,ya'ni eski stringni o'zgartirib yubora olmaydi.

Console.WriteLine(longString.Replace('i', 'I'));
Console.WriteLine(longString.Replace("is", "IS"));
Console.WriteLine(longString.Replace("Is", "IS", StringComparison.CurrentCultureIgnoreCase));
Enter fullscreen mode Exit fullscreen mode

Contains()

Stringning berilgan substringdan tashkil topgan yoki yo'qligini tekshiradi.

Console.WriteLine(longString.Contains('i'));
Console.WriteLine(longString.Contains('I'));
Console.WriteLine(longString.Contains('I', StringComparison.CurrentCultureIgnoreCase));
Console.WriteLine(longString.Contains("very"));
Console.WriteLine(longString.Contains("VERY"));
Console.WriteLine(longString.Contains("VERY", StringComparison.CurrentCultureIgnoreCase));
Enter fullscreen mode Exit fullscreen mode

Joins()

Ikkita stringni bir-biriga qo'shish uchun ishlatiladi

Console.WriteLine(string.Join(", ", [123, "hello", 'a', 3.14]));
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(longString.EndsWith("string"));
Console.WriteLine(longString.EndsWith("STRING"));
Console.WriteLine(longString.EndsWith("STRING", StringComparison.CurrentCultureIgnoreCase));
Enter fullscreen mode Exit fullscreen mode

StartWith()

String berilgan string bilan boshlanishini tekshiradi

Console.WriteLine("StartsWith()");
Console.WriteLine(longString.StartsWith("This"));
Console.WriteLine(longString.StartsWith("this"));
Console.WriteLine(longString.StartsWith("this", StringComparison.CurrentCultureIgnoreCase));
Enter fullscreen mode Exit fullscreen mode

IndexOf()

Belgilangan obyektning stringdagi o'rnini qaytarib beradi

Console.WriteLine("IndexOf()");
Console.WriteLine(longString.IndexOf('i'));
Console.WriteLine(longString.IndexOf("very"));
Console.WriteLine(longString.IndexOf("VERY"));
Console.WriteLine(longString.IndexOf("VERY", StringComparison.CurrentCultureIgnoreCase));
Enter fullscreen mode Exit fullscreen mode

Remove()

String ichidagi qaysidir belgi yoki stringni o'chirib tashlaydi

Console.WriteLine(longString.Remove(10));
Console.WriteLine(longString.Remove(10, 5));
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 qaytaradi

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

PadLeft()

String uchun chap tomondan joy ajratib bo'sh joylarni qaysidir belgi orqali to'ldiradi

Console.WriteLine("PadLeft()");
Console.WriteLine("Hello".PadLeft(10));
Console.WriteLine("Hello".PadLeft(10, '+'));
Enter fullscreen mode Exit fullscreen mode

PadRight()

String uchun o'ng tomondan joy ajratib bo'sh joylarni qaysidir belgi orqali to'ldiradi

Console.WriteLine("PadRight()");
Console.WriteLine("Hello".PadRight(10));
Console.WriteLine("Hello".PadRight(10, '+'));
Enter fullscreen mode Exit fullscreen mode

Compare()

Ikkita stringni bir biri bilan solishtirish uchun ishlatiladi.

Console.WriteLine(string.Compare("A", "B"));
Console.WriteLine(string.Compare("B", "AB"));
Console.WriteLine(string.Compare("B", "B"));
Console.WriteLine("A".CompareTo("B"));
Enter fullscreen mode Exit fullscreen mode

Equals()

Ikkita stringning bir biriga teng yoki teng emasligini tekshiradi

Console.WriteLine(string.Equals("Hello", "hello"));
Console.WriteLine(string.Equals("Hello", "hello", StringComparison.CurrentCultureIgnoreCase));
Console.WriteLine("Hello".Equals("hello", StringComparison.CurrentCultureIgnoreCase));
Enter fullscreen mode Exit fullscreen mode

Top comments (0)