C# Strings5 min read

In C#, String is the series of characters or array of characters that represents a text. It is of a reference type (Object) in c#. Various operations are performed on a string such as concatenation, comparison, getting substring, search, trim, replacement, etc.

  • It is a reference type.
  • It’s immutable that is its state cannot be changed.
  • It can contain nulls.
  • It overloads the operator(==).

Comparing string and String

string and String are the same in C#, the keyword string is an alias of System.String. Both are equivalent and we can use any convention because the string class inherits the properties and methods of the System.String class.

Declaring string using string and System.String class.

A variable of the string type can be declared and assign string literal as given below.

String example in C#


Name: Simple

Roll: 43
Marks: 80

C# String Properties

Charsreturns a char object at a specified position in the current string object.
Lengthreturns the length of a specified string

C# String methods

We can perform an operation on strings in order to manipulate them according to the need. An to do that there are few methods present in C#. The following table shows the most commonly used methods in string.

Compare(String, String)It is used to compare two strings and returns integer value as an output.
Concat(String, String)It is used to concatenate the two specified strings.
Contains(String)It is used to check whether a specified character/string exists or not in the string value.
Copy(String)It creates a new instance of String with the same value as a specified String.
EndsWith(String)It is used to check whether the specified character is the last character of the string or not.
Equals(String, String)It is used to compare two strings. It returns a Boolean value as output.
GetHashCode()It is used to get the HashValue of the specified string.
IndexOf()It is used to get the index position of the first occurrence of the specified character.
ToLower()It is used to transform the specified string into the lower case.
ToUpper()It is used to transform the specified string into the upper case.
ToString()It returns the instance of a String.
Insert()It is used to insert the string/character in the string at the specified position.
Remove()It is used to delete all the characters from beginning to specified index positions.
Replace()It is used to replace the character.
Split()It is used to split the string at the specified value.
StartsWith()It is used to check whether the specified character is the starting character of the string or not.
Substring()It is used to find the substring from a specified string.
ToCharArray()It is used to convert a string into a char array.
Trim()It is used to remove the extra whitespaces from the beginning and end of the specified string.

Let us go through some of the examples of the above methods mentioned in C#.

Comparing String in C#

Strings are not equal.

Concatinate Stringin C#

This is simple2code.com

String Contains method in C#


Substring method in C#


Special Characters

As we have seen till now the string must be within the quotes. Now there might be cases where the string may need a single quote or double quote to form sentences. Example, John’s book, It’s alright, etc.

Now to use quotes within the string quotes, we need to use backslash escape character. With the help of this, we can turn the special character into a string character.

Escape characterResult

Let us go through an example to demonstrate the escape character.

This is "simple2code.com".
It's allright.

As you can see in the output, the result is with the quotes where we inserted in the program. This way you can apply special characters in a string.


C Program to search an element in an array using Pointers

A separate function( search_function()) will be created where the array pointer will be declared and the searched element along with the size of an array …

C Program to find the sum of the digits of a number using recursion function

This C program calculates the sum of digits of a given number using recursion. Here’s a concise explanation: Function Definition: sumDigits(int n) This function calculates …

C program to find factorial of a number using Ternary operator with Recursion

Recursion refers to the function calling itself directly or in a cycle. Before we begin, you should have the knowledge of following in C Programming: …

C Program to Add Two Numbers Using Call by Reference

The program takes the two numbers from the user and passes the reference to the function where the sum is calculated. You may go through …

Find the output ab, cd, ef, g for the input a,b,c,d,e,f,g in Javascript and Python

In this tutorial, we will write a program to find a pairs of elements from an array such that for the input [a,b,c,d,e,f,g] we will …

String Pattern Programs in C

In this tutorial, we will write various C pattern programs for String. Before that, you may go through the following topics in C. for loop …