The string class in C#

String manipulation is one of the most important and frequent operations which you need to perform in any computer program. It starts with asking for an input from the user which is received in the form of a string and has to be converted into other data types like integer, float etc. C# has many useful functions inbuilt to perform string operations like comparing two strings, replacing part of the string, splitting a string etc. But before we can start understanding about all these operations we need to first understand about the string class in C#.

Definition of a string

Let’s revise the definition of a string first. By definition

A string is an array of characters terminated by a null character ()

The above definition is something you need to always keep in mind. This tells you that when dealing with strings you are not dealing with a single object; rather you are dealing with an array of characters.

The string class in C#

To work with strings C# has given you a special String class defined in the System namespace. Also you will be a little bit surprised when working with strings in C# when you will see that you can use the string class using both string (with small s) and String (with capital S) keywords. Why? Because C# gives you an alias for the class String and this alias starts with a small s. The point to understand is that there is no difference between string and String keywords; you can use anyone you like.

String variables are reference types

All the string type variables you create in C# are reference types. This means that you can assign null values to a string type variable.

How to declare a string variable in C#

To declare a string type variable in C# you need to use the following syntax

String str; // creates a string variable and assigns null to it  String str1 = “Hello”; // Creates a string variable and initializes it to Hello  String str2 = “”; // Creates a string variable and initializes it to empty string.

Calculate the length of the string

The string class has a property called length using which you can find the length of any string.  See the code below

static void Main(string[] args)  {          string str = "Hello";                    // Print the length of the string          Console.Write(str.Length);  }

Check if a string is null or empty

When working with strings there are two possible options for which you need to be sure of – null strings and empty strings. These two things are not the same. A null string means a string which has nothing in it and points to null. An empty string means a string which has been assigned an empty value. Writing string str; will create a null string whereas writing string str = “”; will create an empty string.

The string class gives you a very important function IsNullOrEmpty to check if the string is null or empty. Look at the code below

static void Main(string[] args)  {          string str = null;            if (string.IsNullOrEmpty(str))              Console.Write("String is null or empty");          else              Console.Write("String is not empty");  }

The output of this program will be “String is null or empty”. Inside the if statement see carefully how we have used the IsNullOrEmpty method from the string class and passed it the string we want to check.

Using strings as arrays of characters

By definition we understand that strings are arrays of characters. Therefore if you wish you can access any character in the string by specifying the index of that character. Look at the code below

static void Main(string[] args)  {      string str = "Open E-School";        Console.WriteLine("The character at index 0 is : " + str[0]);      Console.WriteLine("The character at index 5 is : " + str[5]);  }

See how we are using the string variable as an array and passing the index of the character we want to access. The output of this program is

Quick Links



Total Followers
Study Group Created
Study Group Joined
Following Teacher
Following Organization
Blog Articles Added
Questions Asked
Questions Answered
Jobs Posted
Total Members in Group
Questions asked by members
Tasks added in this Group

Please wait..


Login to Open ESchool OR Create your account    Login   SignUp