String in .NET C#: Usage, and Example

Introduction

A string in C# is an object belonging to the String class that symbolizes a string of characters. Numerous operations, including length, concatenation, comparison, obtaining substring, search, trim, replacement, and more, can be carried out on strings.

System. The string is the class that represents a string. The "string" keyword is a shorthand for the System. String class; one can use String as a substitute for writing System.String when writing code. Thus, string and String can both be used as systems. String class aliases. String, then, is an instance of the System. String class.

String Properties

  1. It's a type of reference.
  2. It is unchangeable; its state cannot be changed.
  3. It might have nulls in it.
  4. The operator (==) is overloaded.

What distinguishes C#'s string (small) from String (Capital)?

There are two ways to utilize a string in C#: either use the capital S for String or use the minuscule "s" for String, like in the example below.

string firstName = "";
String lastName = "";

Actually, String (Capital string) is the alias of the tiny string. The true class name is a capital string, String, as demonstrated in the example below if you right-click on the small text and select definition.

public sealed class String : IComparable, IClonable, IConvertiable, IEnumerable, IComparable<String>, IEnumerable<char>, IEquatable<String>

Various Methods for String Creation

  1. From a literal, create a string.
  2. Use concatenation to create a string.
  3. Employ a constructor to generate a string.
  4. Use a method or a property to create a string.
  5. Use formatting to create a string.
  6. Use string interpolation to create a string.
  7. Convert a literal into a string
  8. It is the most typical method for making a string. In order to assign the value inside the double quotes, the user must first define the string variable.
string name = "Jaimin Shethiya";
Console.WriteLine("{0} - {1}", nameof(name), name);

string filePath = "C:\\Downloads\\filename.pdf";
Console.WriteLine("\n{0} - {1}", nameof(filePath), filePath);

String Creation

Concatenate a string

In C#, the string concatenation operator "+" can be used to create a string. The string concatenation operator (+) is used to combine or merge one or more strings into a single string from any combination of String instances and string literals.

string firstName = "Jaimin";
string lastName = "Shethiya";
string name = firstName + " " + lastName;

Console.WriteLine("{0} - {1}", nameof(firstName), firstName);
Console.WriteLine("\n{0} - {1}", nameof(lastName), lastName);
Console.WriteLine("\n{0} - {1}", nameof(name), name);

Concatenate a string

Use the constructor to create a string

There are multiple overloaded constructors for the String class that accept an array of characters or bytes. Pointers to character arrays or signed byte arrays are supplied as inputs to some of the constructors.

char[] chars = { 'J', 'a', 'i', 'm', 'i', 'n' };
string firstName = new string(chars);

Console.WriteLine("{0} - {1}", nameof(firstName), firstName);

Create a string

Make a string with a method or a property

To execute a method that consistently yields a string or fetches a property. For instance, extracting a substring from a longer string using methods of the String class.

string firstName = "Jaimin";

string text = firstName.Substring(0, 3);

Console.WriteLine("{0} - {1}", nameof(firstName), firstName);
Console.WriteLine("\n{0} - {1}", nameof(text), text);

Property

Make a string by utilizing Format

To convert a value or object to its string representation, use the "Format" method. A string is the result of the String. Format function.

string firstName = "Jaimin";
string lastName = "Shethiya";

string name = string.Format("{0} {1}", firstName, lastName);

Console.WriteLine("{0} - {1}", nameof(firstName), firstName);
Console.WriteLine("\n{0} - {1}", nameof(lastName), lastName);
Console.WriteLine("\n{0} - {1}", nameof(name), name);

Utilizing Format

String Interpolation

Concatenating strings is better done via string interpolation. To concatenate string variables with static strings, use the + sign.

A unique character $ is included in C# 6 to denote an interpolated string. A blend of static and variable strings, with variable strings enclosed in angle brackets, is called an interpolated string.

string firstName = "Jaimin";
string lastName = "Shethiya";

string name = $"{firstName} {lastName}";

Console.WriteLine("{0} - {1}", nameof(firstName), firstName);
Console.WriteLine("\n{0} - {1}", nameof(lastName), lastName);
Console.WriteLine("\n{0} - {1}", nameof(name), name);

String Interpolation

We learned the new technique and evolved together.

Happy coding!