DEV Community

Cover image for String methods
Sabrina
Sabrina

Posted on

1 1 2 2 2

String methods

Substring() -> returns substring into substring

Console.WriteLine(longstring.Substring(5));
Console.WriteLine(longstring.Substring(5,10));
Console.WriteLine(longstring.Substring(5,10,100));//Error
Enter fullscreen mode Exit fullscreen mode

Error : xatto bo'ladi,sababi - hajmidan oshib ketdi.

Replace() -> replace the specified old character with specified

var longString = "This guy is strongman";
Console.WriteLine(longstring.Replace("is","IS"));
Console.WriteLine(longstring.Replace("very","VERY"));
Console.WriteLine(longstring.Replace("iS","IS", StringComparison.CurrentCultureIgnoreCase));
Enter fullscreen mode Exit fullscreen mode

Join() -> joins the given string using the specified separator;

string [] words = {"apple","banana","cherry"};
string result = string.Join("-",words);
Console.WriteLine(result);

string [] words2 = {"book","pen","notebook"};
string result2 = string.Join("|",words2);
Console.WriteLine(result2);
Enter fullscreen mode Exit fullscreen mode

Remove() -> returns characters from a string
Berilgan tekstdan ochiradi

string tekst = "Hello world";
string tekst1 = tekst.Remove(1,5);
Console.WriteLine(tekst1);
Enter fullscreen mode Exit fullscreen mode

PadLeft() -> returns a string padded with spaces or with specified Unicode character
PadLeft() -> tekstni chap tomonidan nechtadur joy ajratib beradi

string tekst = "Hello world";
string tekst1 = tekst.PadLeft(10,'0');
string tekst2 = tekst.PadLeft(10);
Console.WriteLine(tekst1);
Console.WriteLine(tekst2);
Enter fullscreen mode Exit fullscreen mode

PadRight() -> returns a string padded with spaces or with specified Unicode character
PadRight() -> tekstni ong tomonidan nechtadur joy ajratib beradi

string tekst3 = "Hello world";
string tekst4 = tekst.PadLeft(10,'0');
string tekst5 = tekst.PadLeft(10);
Console.WriteLine(tekst4);
Console.WriteLine(tekst5);
Enter fullscreen mode Exit fullscreen mode

ToCharArray -> converts to string to a char array

string tekst = "Hello";
char [] charArray = tekst.ToCharArray();
foreach(char c in charArray)
{
    Console.WriteLine(c);
}
Enter fullscreen mode Exit fullscreen mode

LastIndexOf() - returns index of the last occurance of specified string

string tekst2 = "Hello world";
int index = tekst2.LastIndexOf("o");
int index2 = tekst2.IndexOf("o");
Console.WriteLine(index);
Console.WriteLine(index2);
Enter fullscreen mode Exit fullscreen mode

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)