Home > C# > C# Strings

Strings in C#

Strings in C# are a sequence of characters. A C# string can have any type of characters in it in a sequence. Usually, strings in c# contain alphanumeric characters. And we write each string in c# is double quotes. Such as:

“Hello”
“This is a C# string. It is similar to string in C++.”

Both the above statements are strings in c#.

How to create a c# string?

We can create a c# string in two ways.

Using an array of characters

A string can be made by using a character array. Each character of a string is placed on each index of the array.
E.g.

char [] my_arrary = new char [] {‘H’,’e’,’l’,’l’,’o’};

As a string can contain spaces. So we can create a string with spaces in C#. Space is also a character in the string. so we place it on an individual index in the array.

char [] my_arrary = new char [] {‘T’,’h’,’i’,’s’,’ ’,’i’,’s’,’ ’,’s’,’t’,’r’,’I’,’n’,’g’,’.’};

Using string keyword of System.String class.

We can create a string using one of the following ways that use string keyword of System.String class.

Assigning a string literal to a String variable

A C# string can be created using Sting literal available in c#.

E.g.

String str = “Hello”;

By using a String class constructor

We can create a string in C# by using the constructor of String class.  This constructor can accept a character array or a string value using double quotes.

E.g.

char [] my_arrary = new char [] {‘H’,’e’,’l’,’l’,’o’};
String str = new string (my_array);

Using the string concatenation operator (+)

We can create a String by concatenating the already available strings using the concatenation operator in c#.

E.g.

// This is the first string in concatination
String str_2 = "This is a string.";
// This is the 2nd string in concatination
String str_3 = "created using concatination operator.";
String str_4 = str_2 + str_3;

By retrieving a property or calling a method that returns a string

A function which returns a string can also result in making a string in c#.

E.g.

String str = Obj. myFunction (); /* obj is the object of the class 
      which has myFunction method defined in it. 
  And myFunction method should return a string value.*/

By calling a formatting method to convert a value or an object to its string representation

E.g.

String str_5 = String.Format("This is String no {0} & This is string no {1}",0,1);

Below example will show us these different ways of creating a C# string.

Example Program

using System;
namespace strings
{
     class Program
     {
          static void Main(string[] args)
          {
               char[] my_array = new char[] {'H','e','l','l','o'};
               Console.WriteLine("This is a string created using Array : "+my_array[0]+
               my_array[1] + my_array[2] + my_array[3] + my_array[4] );

               String str = "Hello world";
               Console.WriteLine("This is a string created using String class variable: " 
               +str);

               String str_1 = new string(my_array);
               Console.WriteLine("This is a string created using String class constructor: " +
                str_1);
               
               // This is the first string in concatination
               String str_2 = "This is a string.";
               // This is the 2nd string in concatination
               String str_3 = "created using concatination operator.";
               String str_4 = str_2 + str_3;
               Console.WriteLine(str_4);

               String str_5 = String.Format("This is String no {0} & This is string no {1}",0,1);
               Console.WriteLine(str_5);
          }
     }
}

Output
Strings in C# - A C# string example
A variety of built-in methods is provided by System.String class. Each method is used for some specific task to be performed with string. Details about string methods can be read here

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.