Namespace: System
String Class
Represents text as a sequence of zero or more 16-bit unsigned integer (UTF-16) code units.
Strings in .NET are immutable, meaning their value cannot be changed after they are created.
Summary
The System.String
class is fundamental to text manipulation in .NET.
It provides a rich set of methods for creating, comparing, searching, and modifying strings.
Because strings are immutable, any operation that appears to modify a string actually creates a new string instance.
Constructors
-
public String(char* value)Initializes a new instance of the String class to the specified pointer to a character array.
-
public String(char* value, int startIndex, int length)Initializes a new instance of the String class to the specified pointer to a character array, a starting index, and a length.
-
public String(char[] value)Initializes a new instance of the String class to the value indicated by a specified array of 16-bit unsigned integer code units.
-
public String(char[] value, int startIndex, int length)Initializes a new instance of the String class to the value indicated by a specified array of 16-bit unsigned integer code units, a starting index, and a count.
-
public String(char[] value, int startIndex, int length, IFormatProvider provider)Initializes a new instance of the String class to the value indicated by a specified array of 16-bit unsigned integer code units, a starting index, a count, and culture-specific formatting information.
Properties
-
public static String EmptyRepresents an empty string. This field is read-only.
-
public int Length { get; }Gets the number of characters in the current String object.
-
public char Chars(int index) { get; }Gets the specified character at position 'index' in the current String object.
Methods
-
public static int Compare(string strA, string strB)Compares two strings.
-
public static int Compare(string strA, string strB, bool ignoreCase)Compares two strings, ignoring or considering case.
-
public static int CompareTo(string other)Compares the current string with another string.
-
public static string Concat(string str0, string str1)Concatenates the specified instances of String.
-
public static string Format(string format, object arg0)Formats a string by using the specified format string and arguments.
-
public bool Contains(string value)Determines whether the string contains a specified substring.
-
public int IndexOf(char value)Returns the zero-based index of the first occurrence of the specified Unicode character within this instance.
-
public int IndexOf(string value)Returns the zero-based index of the first occurrence of a specified substring within this instance.
-
public string ToLower()Converts this instance to lowercase.
-
public string ToUpper()Converts this instance to uppercase.
-
public string Trim()Removes all leading and trailing white space characters from the current String object.
-
public char[] ToCharArray()Copies the characters in this instance to a Unicode character array.
Operators
-
public static bool operator == (string left, string right)Compares two strings for equality.
-
public static bool operator != (string left, string right)Compares two strings for inequality.
-
public static string operator + (string left, string right)Concatenates two strings.
Examples
using System;
public class StringExample
{
public static void Main(string[] args)
{
string greeting = "Hello, World!";
Console.WriteLine(greeting); // Output: Hello, World!
string name = "Alice";
string message = string.Concat("Welcome, ", name, "!");
Console.WriteLine(message); // Output: Welcome, Alice!
string sentence = "The quick brown fox jumps over the lazy dog.";
bool containsFox = sentence.Contains("fox");
Console.WriteLine($"Does the sentence contain 'fox'? {containsFox}"); // Output: Does the sentence contain 'fox'? True
int index = sentence.IndexOf("jumps");
Console.WriteLine($"The word 'jumps' starts at index: {index}"); // Output: The word 'jumps' starts at index: 20
string upperCase = greeting.ToUpper();
Console.WriteLine($"Uppercase version: {upperCase}"); // Output: Uppercase version: HELLO, WORLD!
string trimmed = " Trim me! ";
Console.WriteLine($"Trimmed: '{trimmed.Trim()}'"); // Output: Trimmed: 'Trim me!'
}
}