System.String
The System.String
class represents text as a sequence of Unicode characters. It provides a rich set of methods for manipulating, searching, comparing, and formatting strings.
Namespace
System
Assembly
System.Runtime.dll
Inheritance
System.Object → System.String
Immutability
Instances of String
are immutable; any operation that appears to modify a string actually creates a new instance.
Common Methods
Method | Signature | Description |
---|---|---|
Concat |
static string Concat(params string[] values) |
Combines one or more strings into a single string. |
Contains |
bool Contains(string value, StringComparison comparisonType = StringComparison.CurrentCulture) |
Determines whether the string contains a specified substring. |
IndexOf |
int IndexOf(string value, int startIndex = 0, StringComparison comparisonType = StringComparison.CurrentCulture) |
Returns the zero-based index of the first occurrence of a specified substring. |
Replace |
string Replace(string oldValue, string newValue, StringComparison comparisonType = StringComparison.CurrentCulture) |
Returns a new string in which all occurrences of a specified string are replaced. |
Split |
string[] Split(params char[] separator) |
Splits a string into substrings based on separator characters. |
ToUpper |
string ToUpper(CultureInfo culture = null) |
Converts the string to uppercase using culture-specific rules. |
ToLower |
string ToLower(CultureInfo culture = null) |
Converts the string to lowercase using culture-specific rules. |
Trim |
string Trim(params char[] trimChars) |
Removes all leading and trailing white-space characters from the string. |
Key Properties
Property | Type | Description |
---|---|---|
Length |
int |
Gets the number of characters in the current string. |
Chars |
char |
Gets the character at the specified index. |
Example: Building a CSV line
using System;
using System.Text;
class Program
{
static void Main()
{
var values = new[] { "Alice", "Bob", "Charlie" };
var csv = string.Join(",", values);
Console.WriteLine(csv); // Output: Alice,Bob,Charlie
}
}
Example: Case‑insensitive search
string text = "Hello World";
bool contains = text.Contains("hello", StringComparison.OrdinalIgnoreCase);
Console.WriteLine(contains); // True