In C#, a “string” is a sequence of Unicode characters or an array of characters. Unicode characters range from U+0000 to U+FFFF. The arrangement of characters is also called text. Therefore, a string is a representation of text. Strings are an important concept, but sometimes it can be confusing whether a string is a keyword, an object, or a class. Let’s clarify this concept. Strings are represented by the System.String class. The “string” keyword is an alias for the “System.String” class. Instead of writing System.String, you can use String, which is an abbreviation for the System.String class. Therefore, we can say that both String and String can be used as aliases of the System.String class. Therefore, String is an object of the System.String class.

string s1 = “First Paragraph”;          // creating string using string keyword 
String s2 = “Second Paragraph”;         // creating string using String class 
System.String s3 = “Third Parapraph”;   // creating string using String class 

In other words, a String object is a contiguous collection of System.Char objects that represent Strings. The maximum size of a String object in memory is 2GB, or approximately 1 billion characters. Additionally, the System.String class is immutable. i.e. Once created, it cannot be changed.


using System;
class CodeConfigs 
{
	static void Main(string[] args)
	{

		// declare a string Name using 
		// "System.String" class
		System.String Name;
		
		// initialization of String
		Name = "CodeConfig-1";


		// declare a string id using 
		// using an alias(shorthand) 
		// "String" of System.String
		// class
		String id;
		
		// initialization of String
		id = "101";

		// declare a string marks using 
		// string keyword
		string marks;
		
		// initialization of String
		marks = "98";
		
		// Declaration and initialization of
		// the string in a single line
		string rank = "1";

		// Displaying Result
		Console.WriteLine("Name: {0}", Name);
		Console.WriteLine("Id: {0}", id);
		Console.WriteLine("Marks: {0}", mrk);
		Console.WriteLine("Rank: {0}", rank);
	}
}

Output

Name: CodeConfig-1
Id: 101
Marks: 98
Rank: 1

String Characteristics: 

  • It is a reference type.
  • It’s immutable (its state cannot be altered).
  • It can contain nulls.
  • It overloads the operator (==).

String Class Properties: The String class has two properties as follows: 

  • Chars: It is used to get the Char object at a specified position in the current String object.
  • Length: It is used to get the number of characters in the current String object. To know more about the string class properties please go to String Properties in C#.

Different Ways for Creating a String:

  • Create a string from a literal.
  • Create a string using concatenation.
  • Create a string using a constructor.
  • Create a string using a property or a method.
  • Create a string using formatting.

Let’s see all above strings formation using examples below:

1. Create a string from a literal

This is the most common way to create strings. The user must define a string variable and assign its value within double quotes. You can use any type of character within double quotes, except special characters such as backslash (\).

// C# program to demonstrate the
// string creation using literals
using System;
class CodeConfigs
{

	// Main Method
	static void Main(string[] args)
	{
		string str1 = "CodeConfig.in";
		Console.WriteLine(str1);
		
		
		// using double slash \\ 
		string str2 = "c:\\User\\CodeConfig\\config.cs";
		Console.WriteLine(str2);

	}
}

2. Create a string using concatenation

We can create a string by using string concatenation operator “+” in C#

using System;
class CodeConfig
{
	public static void Main()
	{
		string s1 = "First";
		string s2 = "Second";
		string s3 = "Third";
		string s4 = "Fourth";

		// using concatenation operator
		string str = s1 + s2 + s3 + s4 + "s";

		Console.WriteLine(str);
	}
}

3. Create a string using a constructor

The String class has several overloaded constructors that accept arrays of characters or bytes. Some constructors include pointers to character arrays or signed byte arrays as parameters.

using System;
class CodeConfig
{
	// Main Method
	public static void Main()
	{
		char[] chars = { 'H', 'E', 'L', 'L', 'O'};
		string str1 = new string(chars);
		Console.WriteLine(str1);

		// Create a string that consists of 
		// a character repeated 10 times.
		string str2 = new string('C', 10);
		Console.WriteLine(str2);
		
	
	}
}

4. Create a string using a property or a Method

To retrieving a property or calling a method which always returns a string.

using System;
class CodeConfig
 {
	public static void Main()
	{
		string sentence = "Welcome to CodeConfig";

		// Extract the second word.

		// taking the first space position value
		int startpos = sentence.IndexOf(" ") + 1;

		// taking the second space position value
		int endpos = sentence.IndexOf(" ", startpos) - startpos;

		// now extract second word from the sentence
		string wrd = sentence.Substring(startpos, endpos);

		Console.WriteLine(wrd);
	}
}

5. Create a string using Format

The “Format” method is used to convert the value or object to its string representation. The String.Format method returns a string. 

using System;
class CodeConfig
{
	public static void Main()
	{
		int no = 1021;
		string carName = "FORD";
		string color = "White";
        String power= "1.5 CC"

		// string creation using string.Format method
		string str = string.Format("{0} {1} Cars color " +
					"are {2} and engine power is {3}  ", Convert.ToString(no), carName, color,power);
		Console.WriteLine(str);
	}
}

In this article we tried to explain C# | String with Examples. I hope you enjoyed reading this article. For more information you can check out Microsoft learn.

For more information you can visit our C# Section for more such articles.

Leave a Reply

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

Explore More

Working With File Class In C#

In C#, ‘File’ class provides many static methods for moving, copying, and deleting files. These Static methods involve moving a file and copying and deleting a file also. Let’s check

How to Read a Text File in C#

In C# ‘File’ class provides static methods to read given text file data. The File.ReadAllText() method opens a text file, reads all the text present in the file into a

C# | How to use Interface references

In C#, you’re permitted to make a reference variable of an interface type or in other words, you’re permitted to form an interface reference variable. Such kind of variable can