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

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 …
Read More

Java Program to Find pair of Integers in Array whose sum is given Number

In this tutorial, we will write a program to find a pair of elements from an array whose sum equals a given number in java …
Read More

Program to Print Diamond Alphabet Patterns in C

In this tutorial, we will learn to write a C program to print Diamond patterns using alphabets/characters. However, in this tutorial, we will create a …
Read More

Half Diamond Pattern in C using Alphabets

In this tutorial, we will learn and code the half diamond alphabet patterns in C programming language. However, in this tutorial, we will create a …
Read More

Half Pyramid of Alphabets in C

In this tutorial, we will learn and code alphabet patterns in C programming language specifically the Half pyramid of alphabets in C programming. However, in …
Read More

Inverted Half Pyramid Pattern of Alphabets in C

In this tutorial, we will write a C program to print half Pyramid using alphabets/characters. Before that, you may go through the following topic in …
Read More