Facebook Page Twitter Page LinkedIn Page
× C# Tutorial C# Advanced C# Resources


C# Strings

Strings are one of the most common data types in C#. It is used for storing text. It can be created by enclosing characters in double quotation marks. It can be assigned to a variable using = sign.

string MyString = "Hello World!";

Access character of a String

A character (also called element) of a string can be accessed with it's index number. In C#, index number starts with 0 in forward direction. The below figure and example describe the indexing concept of a string.

String Indexing:

C# String Indexing

The below example describes how to access character of a string using its index number.

using System;
 
class MyProgram {
  static void Main(string[] args) {
    string MyString = "HELLO";
    Console.WriteLine(MyString[1]); 
    Console.WriteLine(MyString[4]); 
  }
}

The output of the above code will be:

E
O   

String Length

A String in C# is an object and the String object has a method called Length which can be used to find the total number of characters in the String.

using System;

class MyProgram {
  static void Main(string[] args) {
    string MyString = "Hello World!";
    Console.WriteLine(MyString.Length);  
  }
}

The output of the above code will be:

12

Check a character in the String

C# String has IndexOf() method which returns index position of first occurrence of specified text in the string. Please note that, C# counts index position from 0.

using System;

class MyProgram {
  static void Main(string[] args) {
    string MyString = "Hello World!";
    Console.WriteLine(MyString.IndexOf("Wor"));  
  }
}

The above code will give the following output:

6

String Concatenation

In C#, two strings can be joined using + operator. Along with this, C# String has Concat() method which can also be used to combine two strings.

using System;
 
class MyProgram {
  static void Main(string[] args) {
    string text_1 = "Learn ";
    string text_2 = "C# ";
    string text_3 = "with AlphaCodingSkills.com";
    Console.WriteLine(text_1 + text_2 + text_3); 
    Console.WriteLine(string.Concat(text_1, text_2, text_3)); 
  }
}

The above code will give the following output:

Learn C# with AlphaCodingSkills.com
Learn C# with AlphaCodingSkills.com

String Interpolation

Another way of joining two strings in C# is string interpolation. It places value of the variable into its placeholder in the string. It automatically places a whitespace between placeholders.

using System;

class MyProgram {
  static void Main(string[] args) {
    string text_1 = "C#";
    string text_2 = "with AlphaCodingSkills.com";
    string MyString = $"I learn {text_1} {text_2}";
    Console.WriteLine(MyString); 
  }
}

The above code will give the following output:

I learn C# with AlphaCodingSkills.com

Special Strings in String

The backslash \ escape character is used to convert special character like single quote, double quote, new line, etc. into the string character. The below mentioned table describes special characters in C#:

Escape CharacterResultExample
\' ' "\'C Sharp\'" is converted into: 'C Sharp'
\" " "\"World\"" is converted into: "World"
\\ \ "A\\C" is converted into: A\C
\n new line "Hello\nJohn" is converted into:
Hello
John
\t Tab "Hello\tMarry" is converted into: Hello    Marry
\b Backspace "Hello C\bSharp" is converted into: Hello Sharp

String Methods

For complete list of string methods, please refer to string methods page. Here, few very common string methods are discussed.

  • ToLower(): Returns string in lowercase
  • ToUpper(): Returns string in uppercase
  • Trim(): Removes whitespaces from start and end of the string
  • Replace(): replace specified character(s) with another specified character(s)

Example: ToLower() and ToUpper() String Methods

using System;
 
class MyProgram {
  static void Main(string[] args) {
    string MyString = "Learn Programming";
    Console.WriteLine(MyString.ToLower()); 
    Console.WriteLine(MyString.ToUpper()); 
  }
}

The above code will give the following output:

learn programming
LEARN PROGRAMMING

Example: Trim() and Replace() String Methods

using System;

class MyProgram {
  static void Main(string[] args) {
    string MyString = "  Learn Programming  ";
    Console.WriteLine(MyString.Trim()); 
    Console.WriteLine(MyString.Replace("Programming", "C#")); 
  }
}

The above code will give the following output:

Learn Programming
  Learn C# 

C# String Methods
MethodDescription
Clone() Make clone of string.
CompareTo() Compare two strings and returns integer value as output. It returns 0 for true and 1 for false.
Contains() The C# Contains method checks whether specified character or string is exists or not in the string value.
EndsWith() Checks whether the string ends with the specified string or not.
Equals() The Equals Method in C# compares two string and returns Boolean value as output.
GetHashCode() This method returns HashValue of specified string.
GetType() It returns the System.Type of current instance.
GetTypeCode() It returns the Stystem.TypeCode for class System.String.
IndexOf() Returns the index position of first occurrence of specified character.
IsNullOrEmpty() Checks whether the specified string is null or empty.
IsNullOrWhiteSpace() Checks whether the specified string is null, empty or contains only whitespaces.
PadLeft() Returns a string that right-aligns the given string by padding with spaces or Unicode character on the left.
PadRight() Returns a string that right-aligns the given string by padding with spaces or Unicode character on the right.
Replace() This method replaces the character.
StartsWith() Checks whether the string starts with the specified string or not.
Substring() Retrieves a substring from a string.
ToCharArray() Converts the character of the string to character array.
ToLower() Converts String into lower case based on rules of the current culture.
ToLowerInvariant() Converts String into lower case based on rules of the invariant culture.
ToString() Converts the value of the instance to a string.
ToUpper() Converts String into Upper case based on rules of the current culture.
ToUpperInvariant() Converts String into Upper case based on rules of the invariant culture.
Trim() Removes all leading and trailing instances of a character(s) / whitespaces from the current string.
TrimEnd() Removes all trailing instances of a character(s) / whitespaces from the current string.
TrimStart() Removes all leading instances of a character(s) / whitespaces from the current string.
Insert() Insert the string or character in the string at the specified position.
IsNormalized() This method checks whether this string is in Unicode normalization form C.
LastIndexOf() Returns the index position of last occurrence of specified character.
Length It is a string property that returns length of string.
Remove() This method deletes all the characters from beginning to specified index position.
Split() This method splits the string based on specified value.