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#

Output:

simple
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.

MethodsDescription
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#

//Output
Strings are not equal.


Concatinate Stringin C#

//Output
This is simple2code.com


String Contains method in C#

//Output
FOUND!
FOUND!


Substring method in C#

//Output
simple2code.com


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.

//Output:
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.


MORE

Keith Number Program in C

In this tutorial, we will write a Keith number in C. It is one of the most asked questions in an interview. Before that, you …
Read More

Keith Number Program in C++

In this tutorial, we will write a Keith number in C++. It is one of the most asked questions in an interview. Before that, you …
Read More

Singly linked list Program C++

In this tutorial, we will write a C++ program to implement singly linked list. In this program, we will create a separate function for each …
Read More

Fascinating Number Program in C

In this tutorial, we will write a C program to check whether the given number is a Fascinating number or not. You may go through …
Read More

Peterson Number in C

In this tutorial, we will learn about the Peterson number and check if the number is Peterson or not in C. What is Peterson Number? …
Read More

Peterson Number in C++

In this tutorial, we will learn about the Peterson number and write a C++ program to check if the given number is a Peterson number …
Read More