Hello, today I would like to tell you some necessary methods for programming.
- First let’s start with the method replace()
Let’s look at the code for this method.
var longString = "this ball is too big";
System.Console.WriteLine(longString.Replace("is", "IS"));
The replace() method is called Replace() and is used to replace text within a string. This method accepts two parameters: the first parameter is the string to be replaced, and the second parameter is the new string.
- The second method is Join()
string[] words = { "apple", "banana", "cherry" };
string result = string.Join("+", words);
System.Console.WriteLine(result);
Join() metodi elementlar to‘plamini bitta qatorga birlashtirish uchun ishlatiladi. Bu metod odatda massivlar yoki ro‘yxatlar kabi kolleksiyalarga qo‘llaniladi. Join() metodi ikkita parametrni qabul qiladi: birinchi parametr elementlar orasiga qo‘shiladigan ajratgich (delimiter), ikkinchi parametr esa birlashtiriladigan elementlar to‘plami.
- The third method is IndexOf()
IndexOf() – prints the address or index of the given text.
string text = "hello world!";
int index = text.IndexOf("o");
System.Console.WriteLine(index);
The IndexOf() method is used to find the index of an element in a string or collection. This method returns the first encountered location (index) of the found element. It returns -1 if the element is not found.
- The next method is Remove()
string text = "Hello world!";
string result = text.Remove(5);
System.Console.WriteLine(result);
The remove() method is used to remove elements from a string or list. There are two main types of remove() methods: one for arrays and one for lists.
- Fifth method *PadLeft()
*
string text = "Hello";
string result = text.PadLeft(10);
System.Console.WriteLine(result);
Separates the given text by several spaces from the left.
- The sixth method *PadRight()
*
string text = "Hello";
string result = text.PadRight(10);
System.Console.WriteLine(result);
Separates the given text by a number of spaces from the right side.
- seventh method LastIndexOf()
string text = "hello world!";
int index = text.LastIndexOf("o");
System.Console.WriteLine(index);
The LastIndexOf() method is used to find the last occurrence (index) of an element in a string or collection.
–> Contains the DateTime structure for working with dates and times in C#.
–> To work with dates and times in C#, create a DateTime structure object using the new keyword.
DateTime now = DateTime.Now;
System.Console.WriteLine(now);
Here it outputs the current time to seconds.
–> DateTime.Now – returns the current system time and date.
–> now – the variable holds the current date and time.
–> calculate the time difference.
DateTime start = new DateTime(2024, 1, 1);
DateTime end = new DateTime(2024, 7, 26);
TimeSpan difference = end - start; System.Console.WriteLine($"Difference: {difference.Days} days");
–> *Difference.Days *– Returns the difference in days using a TimeSpan object.
–> Add and subtract dates.
–> add a day to the day.
DateTime today = DateTime.Today;
DateTime tomorrow = today.AddDays(1); System.Console.WriteLine($"Today: {today.ToShortDateString()})");
System.Console.WriteLine($"Tomorrow: {tomorrow.ToShortDateString()})");
–> Date.Today – returns the current system date, but returns the time components as 00:00:00.
–> Today.AddDays(1) – adds days to the date and returns a new date.
–> Today.ToShortDateString() – will format the date as a string.
–> Today.ToLongDateString() – prints the date in full format.
I think I helped you with some methods.
Discover more from Coursity
Subscribe to get the latest posts sent to your email.