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
- It's a type of reference.
- It is unchangeable; its state cannot be changed.
- It might have nulls in it.
- 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
- From a literal, create a string.
- Use concatenation to create a string.
- Employ a constructor to generate a string.
- Use a method or a property to create a string.
- Use formatting to create a string.
- Use string interpolation to create a string.
- Convert a literal into a string
- 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);
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);
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);
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);
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);
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);
We learned the new technique and evolved together.
Happy coding!