C# Tutorial C# Advanced C# References

C# - Strings



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 figure below describes the indexing concept of a string.

String Indexing:

C# String Indexing

The example below 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 characters in a 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#