VOOZH about

URL: https://www.geeksforgeeks.org/c-sharp/string-in-c-sharp/

⇱ Strings in C# - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Strings in C#

Last Updated : 22 Apr, 2026

In C#, a string is a sequence of Unicode characters (U+0000 to U+FFFF) used to represent text. It is an object of the System.String class. The keyword string and the class name String are both aliases for System.String, so they can be used interchangeably.

Example:

// creating the string using string keyword
string s1 = “GeeksforGeeks”;  

// creating the string using String class
String s2 = “GFG”;  

// creating the string using String class
System.String s3 = “Pro Geek”;  

System.String (defined in the .NET base class library) is a collection of char objects. The maximum length of a string depends on system memory and runtime limitations rather than a fixed size. Strings are immutable, meaning their value cannot be changed after creation.


Output
Name: Geek
Id: 33
Marks: 97
Rank: 1

Key Characteristics of Strings

  • Immutable: Once created, the content of a string cannot be altered. Any modification results in the creation of a new string.
  • Reference Type: Strings are reference types, but they behave like value types in some scenarios, such as comparison.
  • Unicode Support: Strings can contain any Unicode character, allowing support for multiple languages.
  • Null and Embedded Nulls: Strings can be null and may also contain embedded null characters (\0).
  • Operator Overloading: Strings support operator overloading, such as + for concatenation and == for comparison.

String Class Properties

  • Chars: It is used to get the Char object at a specified position in the current String object.
  • Length: It is used to get the number of characters in the current String object. For more details, refer to the String Properties in C# article.

Reading String from User-Input

A string can be read out from the user input. The Console.ReadLine() method is used to read a string from user input. Example:

Input:

Hello Geeks !

Output:

User Entered: Hello Geeks !

Different Ways to Create Strings

Method

Syntax / Example

Create a string from a literal

string str = "GeeksforGeeks";

Create a string using concatenation

string str = str1 + "data";

Create a string using a constructor

// Create a string from a character array
char[] chars = { 'G', 'E', 'E', 'K', 'S' };
string str = new string(chars);

Create a string using a property or a method

// start and end are the index for str index
string substr = str.Substring(start, end);

Create a string using formatting

string str = string.Format("{0} {1} Cars color " + "are {2}", no.ToString(), cname, clr);

Example:


Output
Method 1: Geeks
Method 2: GeeksofGeeks
Method 3: GEEKS
Method 4: For
Method 5: Addition of 1 with 2 is 3

C# String Operations

There are multiple String Operations which we can perform in String in C#. Let us demonstrate the operations using the example as mentioned below:

Example 1: For performing string operation of interpolation


Output
GeeksforGeeks is the Organisation Name.
Length: 39

Example 2: For performing trim, replace and concatenate operation


Output
Element at index 2: E
GeeksforGeeks

In the above two examples, we explored several string methods. Below is a list of commonly used string methods in C#.

Methods of C# String

Method

Description

Return Type

Example

IndexOf

Finds the index of the first occurrence of a specified character or substring.

Integer

text.IndexOf("World");

StartsWith

Checks if a string starts with a specified substring.

Boolean

text.StartsWith("Hello");

EndsWith

Checks if a string ends with a specified substring.

Boolean

text.EndsWith("World!");

ToUpper

Converts a string to uppercase.

String

text.ToUpper();

ToLower

Converts a string to lowercase.

String

text.ToLower();

Split

Splits a string into an array based on a specified delimiter.

String Array

fruits.Split(',');

Join

Combines an array of strings into a single string with a specified delimiter.

String

string.Join(" - ", fruitArray);

Contains

Checks if a string contains a specified substring.

Boolean

text.Contains("World");

PadLeft

Pads a string with spaces or a specified character to a certain length.

String

text.PadLeft(20, '*');

PadRight

Pads a string on the right with spaces or a specified character to a certain length.

String

text.PadRight(20, '*');

Remove

Removes characters from a string starting at a specified index.

String

text.Remove(5, 7);

Insert

Inserts a string at a specified index.

String

text.Insert(5, " Beautiful");

Trim

Removes leading and trailing whitespaces.

String

text.Trim();

Replace

Replaces occurrences of a substring with another substring.

String

text.Replace("fun", "awesome");

String Array

We can also create the array of string and assigns values to it. The string arrays can be created as follows:

String [] array_variable = new String[Length_of_array]


Output
value at Index position 0 is Geeks
value at Index position 1 is For
value at Index position 2 is Geeks

String vs System.String 

Aspectsstring (Keyword)System.String (Class)
DefinitionAlias for System.String.Fully qualified class name in .NET.
PerformanceNo difference in performance.No difference in performance.
UsageCommonly used for declaring variables, fields and properties.Used for accessing static methods or fully qualifying types.
Ease of UseProvides a shorthand for writing code.More verbose but functionally identical to string.
Accessing MethodsMethods are accessed via the System.String class.Static methods like String.Substring, String.IndexOf, etc., are accessed directly.
Keyword or ClassC# keyword..NET class.

Note: In .NET, the text is stored as a sequential collection of the Char objects so there is no null-terminating character at the end of a C# string. Therefore a C# string can contain any number of embedded null characters (‘\0’). 

Comment
Article Tags:
Article Tags:

Explore