Format()
Stringni formatlash uchun ishlatiladi
var testString = string.Format("Hello {0}", 123123);
Console.WriteLine("Hello {0:C}", 123123);
output:Hello 123123
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);
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));
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));
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));
Joins()
Ikkita stringni bir-biriga qo’shish uchun ishlatiladi
Console.WriteLine(string.Join(", ", [123, "hello", 'a', 3.14]));
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());
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));
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));
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));
Remove()
String ichidagi qaysidir belgi yoki stringni o’chirib tashlaydi
Console.WriteLine(longString.Remove(10));
Console.WriteLine(longString.Remove(10, 5));
ToUpper()
Katta harfga aylantirib qo’yadi
Console.WriteLine("ToUpper()");
Console.WriteLine(longString.ToUpper());
ToLower()
Kichik harfga o’zgartirib qaytaradi
Console.WriteLine("ToLower()");
Console.WriteLine(longString.ToLower());
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, '+'));
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, '+'));
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"));
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));
Discover more from Coursity
Subscribe to get the latest posts sent to your email.