DEV Community

firdavs090
firdavs090

Posted on

C# {String Methods}

Length:

1)Length: Gets the number of characters in the string.

string str = "Hello, World!";
int length = str.Length; // 13
Enter fullscreen mode Exit fullscreen mode

Accessing Characters:

1)indexer: Gets the character at a specified index.

char ch = str[0]; // 'H';
Enter fullscreen mode Exit fullscreen mode

Comparison:

1)Equals: Compares two strings for equality.

bool isEqual = str.Equals("Hello, World!"); // true
Enter fullscreen mode Exit fullscreen mode

CompareTo:
1)Compares two strings lexicographically.

int comparison = str.CompareTo("Hello"); // > 0
Enter fullscreen mode Exit fullscreen mode

Compare:
1)Compares two strings with optional culture, case, and sort rules.

int comparison = string.Compare(str, "Hello"); // > 0
Enter fullscreen mode Exit fullscreen mode

Searching:

Contains: Determines whether a substring occurs within the string.
Enter fullscreen mode Exit fullscreen mode
bool contains = str.Contains("World"); // true
Enter fullscreen mode Exit fullscreen mode

StartsWith: Determines whether the string starts with a specified substring.

bool startsWith = str.StartsWith("Hello"); // true
Enter fullscreen mode Exit fullscreen mode

EndsWith: Determines whether the string ends with a specified substring.

bool endsWith = str.EndsWith("World!"); // true
Enter fullscreen mode Exit fullscreen mode

IndexOf: Returns the index of the first occurrence of a specified substring.

int index = str.IndexOf("World"); // 7
Enter fullscreen mode Exit fullscreen mode

LastIndexOf: Returns the index of the last occurrence of a specified substring.

int lastIndex = str.LastIndexOf("o"); // 8
Enter fullscreen mode Exit fullscreen mode

Modification:

Substring: Extracts a substring from the string.

string sub = str.Substring(7, 5); // "World"
Enter fullscreen mode Exit fullscreen mode

Replace: Replaces all occurrences of a specified substring with another substring.

string replaced = str.Replace("World", "C#"); // "Hello, C#!"
Enter fullscreen mode Exit fullscreen mode

Remove: Removes a specified number of characters from the string.

string removed = str.Remove(5, 7); // "Hello!"
Enter fullscreen mode Exit fullscreen mode

Insert: Inserts a specified substring at a specified index.

string inserted = str.Insert(7, "beautiful "); // "Hello, beautiful World!"
Enter fullscreen mode Exit fullscreen mode

Case Conversion:

ToUpper: Converts the string to uppercase.

string upper = str.ToUpper(); // "HELLO, WORLD!"
Enter fullscreen mode Exit fullscreen mode

ToLower: Converts the string to lowercase.

string lower = str.ToLower(); // "hello, world!"
Enter fullscreen mode Exit fullscreen mode

Trimming:

Trim: Removes all leading and trailing white-space characters from the string.

string trimmed = str.Trim();
Enter fullscreen mode Exit fullscreen mode

TrimStart: Removes all leading white-space characters from the string.

string trimmedStart = str.TrimStart();
Enter fullscreen mode Exit fullscreen mode

TrimEnd: Removes all trailing white-space characters from the string.

string trimmedEnd = str.TrimEnd();
Enter fullscreen mode Exit fullscreen mode

Splitting and Joining:

Split: Splits the string into an array of substrings based on a specified delimiter.

string[] words = str.Split(' '); // {"Hello,", "World!"}
Enter fullscreen mode Exit fullscreen mode

Join: Concatenates an array of strings into a single string, with an optional separator.

string joined = string.Join(" ", words); // "Hello, World!"
Enter fullscreen mode Exit fullscreen mode

Format:

Format: Replaces the format items in a string with the string representation of specified objects.

string formatted = string.Format("Welcome, {0}!", "John"); // "Welcome, John!"
Enter fullscreen mode Exit fullscreen mode

Top comments (0)