Length:
1)Length: Gets the number of characters in the string.
string str = "Hello, World!";
int length = str.Length; // 13
Accessing Characters:
1)indexer: Gets the character at a specified index.
char ch = str[0]; // 'H';
Comparison:
1)Equals: Compares two strings for equality.
bool isEqual = str.Equals("Hello, World!"); // true
CompareTo:
1)Compares two strings lexicographically.
int comparison = str.CompareTo("Hello"); // > 0
Compare:
1)Compares two strings with optional culture, case, and sort rules.
int comparison = string.Compare(str, "Hello"); // > 0
Searching:
Contains: Determines whether a substring occurs within the string.
bool contains = str.Contains("World"); // true
StartsWith: Determines whether the string starts with a specified substring.
bool startsWith = str.StartsWith("Hello"); // true
EndsWith: Determines whether the string ends with a specified substring.
bool endsWith = str.EndsWith("World!"); // true
IndexOf: Returns the index of the first occurrence of a specified substring.
int index = str.IndexOf("World"); // 7
LastIndexOf: Returns the index of the last occurrence of a specified substring.
int lastIndex = str.LastIndexOf("o"); // 8
Modification:
Substring: Extracts a substring from the string.
string sub = str.Substring(7, 5); // "World"
Replace: Replaces all occurrences of a specified substring with another substring.
string replaced = str.Replace("World", "C#"); // "Hello, C#!"
Remove: Removes a specified number of characters from the string.
string removed = str.Remove(5, 7); // "Hello!"
Insert: Inserts a specified substring at a specified index.
string inserted = str.Insert(7, "beautiful "); // "Hello, beautiful World!"
Case Conversion:
ToUpper: Converts the string to uppercase.
string upper = str.ToUpper(); // "HELLO, WORLD!"
ToLower: Converts the string to lowercase.
string lower = str.ToLower(); // "hello, world!"
Trimming:
Trim: Removes all leading and trailing white-space characters from the string.
string trimmed = str.Trim();
TrimStart: Removes all leading white-space characters from the string.
string trimmedStart = str.TrimStart();
TrimEnd: Removes all trailing white-space characters from the string.
string trimmedEnd = str.TrimEnd();
Splitting and Joining:
Split: Splits the string into an array of substrings based on a specified delimiter.
string[] words = str.Split(' '); // {"Hello,", "World!"}
Join: Concatenates an array of strings into a single string, with an optional separator.
string joined = string.Join(" ", words); // "Hello, World!"
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!"
Top comments (0)