logo codesdope

Strings in C#


We have studied a lot about programming but in the real world, we deal with sentences. We can use strings to introduce English sentences in our program. Let's see how.

As stated earlier, a string is a collection of characters. We know that we write a string inside " ".

A string is not a data type but a class in System namespace in C# i.e., System.String and string is an alias (another name) of the same. So, we can use string and String interchangeably.

Let's look at an example.

using System;

class Test
{
  static void Main(string[] args)
  {
    string s = "CodesDope";

    Console.WriteLine(s);
  }
}
Output
CodesDope

As stated above, a string is a collection of character. So, we can use an array of characters to make a string also.

using System;

class Test
{
  static void Main(string[] args)
  {
    char[] charArray = {'C', 'o', 'd', 'e', 's', 'D', 'o', 'p', 'e'};
    String s = new String(charArray);

    Console.WriteLine(s);
  }
}
Output
CodesDope

In this example, we created a string using an array of characters - String s = new String(charArray); using the new keyword. Take a note that we have used String here but we could also use string as they are same.

Similar to an array, we can also access the characters of a string using stringName[index]. Let's take an example.

using System;

class Test
{
  static void Main(string[] args)
  {
    string s = "CodesDope";

    Console.WriteLine(s[0]);
    Console.WriteLine(s[1]);
    Console.WriteLine(s[2]);
    Console.WriteLine(s[3]);
    Console.WriteLine(s[4]);
  }
}
Output
C
o
d
e
s

+, String Concatenation


As stated earlier, we can use + to join two strings. Let's take an example.

using System;

class Test
{
  static void Main(string[] args)
  {
    string a = "Codes";
    string b = "Dope";
    string c = a+b;

    Console.WriteLine(c);
  }
}
Output
CodesDope

Immutability of String


Strings in C# are immutable i.e., they can't be change after they are created. Whenever it seems that any method or operation is changing a string, it is basically creating a new string object.

using System;

class Test
{
  static void Main(string[] args)
  {
    string a = "Codes";
    string b = "Dope";
    a = a+b;

    Console.WriteLine(a);
  }
}
Output
CodesDope

In this example, it seems that we have changed the string a using a = a+b but actually it has created a new string.

Property and Methods in String


There are many methods and properties available in C# to use on a string. Let's have a look at some of the important properties and methods.

Length


Length is a property of a string which gives us the size or length (number of characters) of a string.

using System;

class Test
{
  static void Main(string[] args)
  {
    string a = "CodesDope";

    Console.WriteLine(a.Length);
  }
}
Output
9

Concat


We use Concat function to join two, three or four strings. If we ever need to join more than 4 strings, we can also pass an array of strings to it.

using System;

class Test
{
  static void Main(string[] args)
  {
    string a = "Codes";
    string b = "Dope";

    String c = String.Concat(a, b);

    Console.WriteLine(c);
  }
}
Output
CodesDope
using System;

class Test
{
  static void Main(string[] args)
  {
    string[] a = {"Co", "de", "sD", "op", "e"};

    String b = String.Concat(a);

    Console.WriteLine(b);
  }
}
Output
CodesDope

Compare


Compare is a function used to compare two strings and returns an integer. If the function is returning 0, it means that both string are in same sort order. If the function is returning a negative value, it means that the first string appears before the second string. It the function is returning a positive value, it means that the first string is suceeding the second string.

using System;

class Test
{
  static void Main(string[] args)
  {
    string a = "Codes";
    string b = "Dope";

    int x = String.Compare(a, b);
    int y = String.Compare(b, a);

    Console.WriteLine(x);
    Console.WriteLine(y);
  }
}
Output
-1
1

We can also pass a boolean in the Compare function indicating whether to ignore case while comparing or not. If we pass true, then the case will be ignored, otherwise not.

using System;

class Test
{
  static void Main(string[] args)
  {
    string a = "CodesDope";
    string b = "codesdope";

    int x = String.Compare(a, b);
    int y = String.Compare(a, b, true);

    Console.WriteLine(x);
    Console.WriteLine(y);
  }
}
Output
1
0

Equals


Equals is used to check if two strings have same value or not.

using System;

class Test
{
  static void Main(string[] args)
  {
    string a = "Codes";
    string b = "Dope";

    Console.WriteLine(a.Equals(b));
  }
}
Output
False

IndexOf


We can use IndexOf method to get the index of any character of a string.

using System;

class Test
{
  static void Main(string[] args)
  {
    string a = "CodesDope";

    Console.WriteLine(a.IndexOf('D'));
  }
}
Output
5

LastIndexOf


If a character is appearing more than one time, we can use LastIndexOf to get the index where it appeared last.

using System;

class Test
{
  static void Main(string[] args)
  {
    string a = "CodesDope";

    Console.WriteLine(a.LastIndexOf('o'));
  }
}
Output
6

Replace


Replace is used to replace all occurances of a particular character or a string with a different one.

using System;

class Test
{
  static void Main(string[] args)
  {
    string a = "CodesDope";
    string b = a.Replace('o', 'd');

    Console.WriteLine(b);
  }
}
Output
CddesDdpe
using System;

class Test
{
  static void Main(string[] args)
  {
    string a = "CodesDope";
    string b = a.Replace("Dope", "Codes");

    Console.WriteLine(b);
  }
}
Output
CodesCodes

Substring


Substring is used to get a part of a string starting from a specified index.

using System;

class Test
{
  static void Main(string[] args)
  {
    string a = "CodesDope";
    string b = a.Substring(5);

    Console.WriteLine(b);
  }
}
Output
Dope

We can also pass the length of the substring which we want along with the starting index.

using System;

class Test
{
  static void Main(string[] args)
  {
    string a = "CodesDope";
    string b = a.Substring(5, 2);

    Console.WriteLine(b);
  }
}
Output
Do

Here, 2 in a.Substring(5, 2) is the length of the substring.

ToLower


It converts all the characters to lowercase.

using System;

class Test
{
  static void Main(string[] args)
  {
    string a = "CodesDope";
    string b = a.ToLower();

    Console.WriteLine(b);
  }
}
Output
codesdope

ToUpper


It converts all character of a string to uppercase.

using System;

class Test
{
  static void Main(string[] args)
  {
    string a = "CodesDope";
    string b = a.ToUpper();

    Console.WriteLine(b);
  }
}
Output
CODESDOPE

There are many other methods of a string. You can take a look at the offical documentation.


Doubt? Ask question
Close

Welcome.please sign up.

Close

Welcome.please login.