Last Updated : 11 Jul, 2025
In C#, a string is a sequence of Unicode characters or an array of characters. The range of Unicode characters will be U+0000 to U+FFFF. The array of characters is also termed as the text. So the string is the representation of the text.
A string is represented by a class System.String. The String class is defined in the .NET base class library. In other words, a String object is a sequential collection of System.Char objects, which represent a string.
Characteristics of String Class:
Example: Use of String class to print a message.
C#
// C# Program to demonstrate the use of String class
using System;
class Geeks
{
public static void Main()
{
// Using String class
String s = "Hello Geek";
// Display the output
System.Console.WriteLine(s);
}
}
Constructor Constructor Description String(Char*) Initializes a new instance of the String class to the value indicated by a specified pointer to an array of Unicode characters. String(Char*, Int32, Int32) Initializes a new instance of the String class to the value indicated by a specified pointer to an array of Unicode characters, a starting character position within that array, and a length. String(Char*, Int32) Initializes a new instance of the String class to the value indicated by a specified Unicode character repeated a specified number of times. String(Char[]) Initializes a new instance of the String class to the value indicated by an array of Unicode characters. String(Char[], Int32, Int32) Initializes a new instance of the String class to the value indicated by an array of Unicode characters, a starting character position within that array, and a length. String(SByte) Initializes a new instance of the String class to the value indicated by a pointer to an array of 8-bit signed integers. String(SByte*, Int32, Int32) Initializes a new instance of the String class to the value indicated by a specified pointer to an array of 8-bit signed integers, a starting position within that array, and a length. String(SByte*, Int32, Int32, Encoding) Initializes a new instance of the String class to the value indicated by a specified pointer to an array of 8-bit signed integers, a starting position within that array, a length, and an Encoding object.
Example: Creating a string using String class constructor String(Char*, Int32) and String([]).
C#
// C# program to create
// string using the constructor String(Char*, Int32), and String(Chars[]);
using System;
class Geeks
{
public static void Main()
{
char[] chars = { 'G', 'E', 'E', 'K', 'S' };
// Create a string from a character array.
string s = new string(chars);
Console.WriteLine(s);
// Create a string that consists of
// a character repeated 5 times.
string s2 = new string('E', 5);
Console.WriteLine(s2);
}
}
Properties
In C# the String class has two properties.
Example: Using the Properties of string class.
C#
// C# program to demonstrate the
// String Class Properties
using System;
class Geeks
{
public static void Main()
{
string str = "GeeksforGeeks";
// using Chars[Int32] & Length property
for (int i = 0; i <= str.Length - 1; i++)
Console.Write("{0} ", str[i]);
}
}
G e e k s f o r G e e k sMethods Method Description Clone() Returns a reference to this instance of String. Compare() Used to compare the two string objects. CompareOrdinal(String, Int32, String, Int32, Int32) Compares substrings of two specified String objects by evaluating the numeric values of the corresponding Char objects in each substring. CompareOrdinal(String, String) Compares two specified String objects by evaluating the numeric values of the corresponding Char objects in each string. CompareTo() Compare the current instance with a specified Object or String object. Concat() Concatenates one or more instances of String, or the String representations of the values of one or more instances of Object. Contains(String) Returns a value indicating whether a specified substring occurs within this string. Copy(String) Creates a new instance of String with the same value as a specified String. CopyTo(Int32, Char[], Int32, Int32) Copies a specified number of characters from a specified position in this instance to a specified position in an array of Unicode characters. EndsWith() Determines whether the end of this string instance matches a specified string. Equals() Determines whether two String objects have the same value. Format() Converts the value of objects to strings based on the formats specified and inserts them into another string. GetEnumerator() Retrieves an object that can iterate through the individual characters in this string. GetHashCode() Returns the hash code for this string. GetType() Gets the Type of the current instance.
Example: Using Copy() and Compare() methods from the String class.
C#
// C# program to illustrate
// String class methods
using System;
class Geeks
{
static void copymethod()
{
string s = "GeeksforGeeks";
string s2 = "geeks";
Console.WriteLine("Original Strings: str1 = " + "'{0}' and str2 ='{1}'", s, s2);
Console.WriteLine("");
Console.WriteLine("After Copy method");
Console.WriteLine("");
// using the Copy method
// to copy the value of str1
// into str2
s2 = String.Copy(s);
Console.WriteLine("Strings are str1 = " + "'{0}' and str2='{1}'", s, s2);
}
// Main method
static public void Main()
{
// variables
string s1 = "geeksforgeeks";
string s2 = "geeksforgeeks";
bool result;
// Compare(string, string) method return true
// because the given strings are equal
result = String.Compare(s1, s2) == 0;
Console.WriteLine("Result of Compare Method: " + result);
// calling method
copymethod();
}
}
Result of Compare Method: True Original Strings: str1 = 'GeeksforGeeks' and str2 ='geeks' After Copy method Strings are str1 = 'GeeksforGeeks' and str2='GeeksforGeeks'Operators
There are two operators string class
Example: Using Equality and Inequality operators of string class.
C#
// Using the Operators of string class
using System;
class Geeks
{
public static void Main()
{
string s1 = "WelcomeToGeeks";
string s2 = "WelcomeToGeeks";
bool res;
// Equality operator return true
// as both string are equal
res = s1 == s2;
Console.WriteLine($"The strings \"{s1}\" and \"{s2}\" equal: {res}");
// Inequality operator return false
// as both string are equal
res = s1 != s2;
Console.WriteLine($"The strings \"{s1}\" and \"{s2}\" not equal: {res}");
}
}
The strings "WelcomeToGeeks" and "WelcomeToGeeks" equal: True The strings "WelcomeToGeeks" and "WelcomeToGeeks" not equal: False
Important Points:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4