String
System
Class String
Represents text as a sequence of UTF-16 code units. The String class is immutable, meaning that once an object of the String class is created, the value of that string cannot be changed. Although the value of a string object cannot be changed, you can still create new string objects to represent modified versions of an existing string.
Namespace: System
Assembly: System.Runtime.dll
Members
Constructors
public String(char* value);
Initializes a new instance of the String class to the specified Unicode character array.
public String(char c, int count);
Initializes a new instance of the String class to the specified Unicode character, and repeats the character a specified number of times.
Properties
public int Length { get; }
Gets the number of characters in the current String object.
Methods
public static String Concat(String str0, String str1);
Concatenates the string representation of the specified objects.
string firstName = "John";
string lastName = "Doe";
string fullName = String.Concat(firstName, " ", lastName);
// fullName is now "John Doe"
public static int Compare(String strA, String strB);
Compares two strings and returns an integer that indicates whether the first string precedes, is equal to, or follows the second string in sort order.
string s1 = "apple";
string s2 = "banana";
int comparison = String.Compare(s1, s2);
if (comparison < 0)
{
Console.WriteLine($"'{s1}' comes before '{s2}'");
}
public bool Contains(String value);
Determines whether the specified string occurs within this string.
string message = "Hello, World!";
bool hasWorld = message.Contains("World"); // true
public int IndexOf(String value);
Returns the zero-based index of the first occurrence of the specified string within the current string.
string text = "This is a sample text.";
int index = text.IndexOf("sample"); // 10
public String ToLower();
Converts this string to lowercase.
string mixedCase = "MiXeD CaSe";
string lowerCase = mixedCase.ToLower(); // "mixed case"
public String ToUpper();
Converts this string to uppercase.
string mixedCase = "MiXeD CaSe";
string upperCase = mixedCase.ToUpper(); // "MIXED CASE"
public String Trim();
Removes all leading and trailing white space characters from the current String object.
string spacedString = " Trim me ";
string trimmed = spacedString.Trim(); // "Trim me"
public String Replace(String oldValue, String newValue);
Replaces all occurrences of a specified string in this instance with another specified string.
string original = "Hello planet, hello universe!";
string replaced = original.Replace("hello", "hi"); // "Hi planet, hi universe!"
public Char[] ToCharArray();
Copies the characters into a new character array.
string word = "Code";
char[] chars = word.ToCharArray(); // ['C', 'o', 'd', 'e']
Operators
public static bool operator ==(String a, String b);
Compares two string objects for equality.
public static bool operator !=(String a, String b);
Compares two string objects for inequality.