C# Strings-Concatenation,Interpolation,Access Strings & Special characters

C# Strings

Text can be stored in strings.

A group of characters enclosed in double quotations makes into a string variable:

Example

Make a string-type variable and give it a value:

string greeting = “Hello”;

If desired, a string variable can have a large word count:

Example

      string greeting2 = “Nice to meet you!”;

String Length

In C#, a string is actually an object with attributes and methods that may be used to manipulate strings. For instance, the Length property can be used to determine a string’s length:

Example

string txt = “ABCDEFGHIJKLMNOPQRSTUVWXYZ”;

Console.WriteLine(“The length of the txt string is: ” + txt.Length);

Other Methods

Various string methods exist, such as ToUpper() and ToLower(), which yield a duplicate of the string in either upper- or lowercase form:

Example

       string txt = “Hello World”;
      Console.WriteLine(txt.ToUpper());   // Outputs “HELLO WORLD”
      Console.WriteLine(txt.ToLower());   // Outputs “hello world”

String Concatenation

You can join strings together by using the + operator. Concatenation is the term for this:

Example

      string firstName = “John “;
      string lastName = “Doe”;
      string name = firstName + lastName;
      Console.WriteLine(name);
 
As you can see, a space has been placed after “John” to separate the first and last names on print.
 

You can also use the string.Concat() method to concatenate two strings:

      string firstName = “John “;

      string lastName = “Doe”;

      string name = string.Concat(firstName, lastName);

      Console.WriteLine(name);

Adding Numbers and Strings

WARNING!

The + operator is used in C# for both concatenation and addition. Recall that numbers are increased. Concatenated strings are used.

A number is the outcome of adding two numbers:

Example

      int x = 10;

      int y = 20;

      int z = x + y;

      Console.WriteLine(z);

A string concatenation is produced when two strings are added:

Example

     string x = “10”;
      string y = “20”;
      string z = x + y;
      Console.WriteLine(z);

String Interpolation

String interpolation is an additional method of string concatenation that inserts variable values into string placeholders. Take note that, unlike with concatenation, you do not need to bother about spaces:

Example

     string firstName = “John”;
      string lastName = “Doe”;
      string name = $”My full name is: {firstName} {lastName}”;
      Console.WriteLine(name);

Access Strings

A string’s characters can be accessed by using the index number enclosed in square brackets [].
The first character in myString is printed in this example:

Example

      string myString = “Hello”;
      Console.WriteLine(myString[0]); // Outputs “H”

Note that the first string index is 0: The initial character is [0]. The second character is [1], and so on.

The second character (1) in myString is printed in this example:

Example

      string myString = “Hello”;
      Console.WriteLine(myString[1]);
   
Using the IndexOf() method, you can also determine the index position of a particular character in a string:

Example

       string myString = “Hello”;
      Console.WriteLine(myString.IndexOf(“e”));
 
Substring() is another helpful function that creates a new string by extracting characters from a string beginning at a certain character position or index. Frequently, this technique is combined with IndexOf() to obtain the precise character position:
 
// Full name
      string name = “John Doe”;
 
      // Location of the letter D
      int charPos = name.IndexOf(“D”);
 
      // Get last name
      string lastName = name.Substring(charPos);
 
      // Print the result
      Console.WriteLine(lastName);

Strings – Special Characters

Due to the requirement that strings be put inside quotes, C# will interpret this string incorrectly and produce an error:
string txt =”We are the so-called “Vikings” from the north.”; The backslash escape character is the way to get around this issue.   Special characters become string characters when you use the backslash (\) escape character:
Escape character Result Description
\’ Single quote
\” Double quote
\\ \ Backslash
A double quote is inserted into a string using the sequence \”:

ExampleGet

string txt = “We are the so-called \”Vikings\” from the north.”; A single quote is inserted into a string using the sequence \’:

Example

string txt = “It\’s alright.” A single backslash is inserted into a string via the sequence \\:

Example

      string txt = “The character \\ is called backslash.”;
In C#, additional helpful escape characters are:
Code Result
\n New Line
\t Tab
\b Backspace