C# | String class - GeeksforGeeks

23 Jan, 2019
In C#, a string is a sequence of Unicode characters or 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. The maximum size of the String object in memory can be 2GB or about 1 billion characters.
Characteristics of String Class:
The System.String class is immutable, i.e once created its state cannot be altered.
With the help of length property, it provides the total number of characters present in the given string.
String objects can include a null character which counts as the part of the string’s length.
It provides the position of the characters in the given string.
It allows empty strings. Empty strings are the valid instance of String objects that contain zero characters.
A string that has been declared but has not been assigned a value is null. Attempting to call methods on that string throws a NullReferenceException.
It also supports searching strings, comparison of string, testing of equality, modifying the string, normalization of string, copying of strings, etc.
It also provides several ways to create strings like using a constructor, using concatenation, etc.
Example:
// C# program to demonstrate the creation// of string using the constructorusing System;class Geeks {      // Main Method    public static void Main()    {        char[] chars = { 'G', 'E', 'E', 'K', 'S' };          // Create a string from a character array.        string str1 = new string(chars);        Console.WriteLine(str1);          // Create a string that consists of        // a character repeated 5 times.        string str2 = new string('E', 5);        Console.WriteLine(str2);    }}
GEEKS
EEEEE

Example:
// C# program to demonstrate the// String Class Propertiesusing System;  class Geeks {       // Main Method    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 s

Example:
// C# program to illustrate// String class methodsusing System;   class GFG {          static void copymethod()    {        string str1 = "GeeksforGeeks";        string str2 = "geeks";        Console.WriteLine("Original Strings: str1 = "                              + "'{0}' and str2 ='{1}'",                          str1, str2);           Console.WriteLine("");           Console.WriteLine("After Copy method");        Console.WriteLine("");           // using the Copy method        // to copy the value of str1        // into str2        str2 = String.Copy(str1);           Console.WriteLine("Strings are str1 = "                              + "'{0}' and str2='{1}'",                          str1, str2);    }             // Main method    static public void Main()    {           // variables        string str1 = "geeksforgeeks";        string str2 = "geeksforgeeks";           bool result;           // Compare(string, string) method return true        // because the given strings are equal        result = String.Compare(str1, str2) == 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'

Example:
// C# program to illustrate the// Equality and Inequality operatorusing System;  class GFG {      // Main Method    public static void Main(string[] args)    {        string s1 = "WelcomeToGeeks";        string s2 = "WelcomeToGeeks";        bool result1, result2;          // Equality operator return true        // as both string are equal        result1 = s1 == s2;          // Inequality operator return false        // as both string are equal        result2 = s1 != s2;          Console.WriteLine(result1);        Console.WriteLine(result2);    }}
True
False

Reference:
https://docs.microsoft.com/en-us/dotnet/api/system.string?view=netframework-4.7.2#definition
CSharp-string
C#
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Difference between Abstract Class and Interface in C#
String.Split() Method in C# with Examples
C# | How to check whether a List contains a specified element
C# | IsNullOrEmpty() Method
C# | Arrays of Strings
C# | Delegates
C# | Abstract Classes
C# | Replace() Method
Extension Method in C#
C# | String.IndexOf( ) Method | Set - 1