Strings

Strings are group or sequence of characters. Strings are immutable, meaning once created, they cannot be changed. These are surrounded by double quotes.

String firstname = “My Name”;

We can also add two strings by + sign as, string firstname = “FirstName” + “ “ + “last name”;

You can also declare same statement as, string name = string.Format(“{0} {1}”, firstname, lastname);

We are calling a .net class called string, which has a format method. So we can directly call string.format

Join strings

We can join strings using join method of string class as:

static void Main(string[] args)
         {
             var numbers = new int[3] {1,2,3} ;
             string list = string.Join(",", numbers);
             Console.WriteLine(list);

             var characters = new char[2] { 'a', 'b' };
             string John = string.Join("$", characters);
             Console.WriteLine(John);
         }

When you use join method, first argument should be a separator and second argument should be an array. Now we can pull each character of the string using

John[0]; // result would be a. John[1]; // result would be $. John[2]; // result would be b. Because John string has a $ b values.

You cannot change the value of elements in array like, John[0]=‘c’; because Johnis declared as string, and values in strings cannot be changed. We can read the values using [0], [1] etc but we cannot change them.

Escape Characters:

\n new line

\’ single quotation

\” double quotation

\t tab

\\ backslash

We just have to add a \ before each special character so that csharp compiler doesnt treat it as is. ex: n is for new line, add \n so that csharp compiler treats it as new line. Same follows for all escape characters given above.

Verbatim strings in C# - Use of @ symbol in string literals

A String can be created as a verbatim string. Verbatim strings start with @ symbol. The C# compiler understand this type of string as verbatim. Basically the @ symbol tells the string constructor to ignore escape characters and line breaks.

String Str1 = "";

Str1 = "\\MyServer\TestFolder\NewFolder";

In statement above the compiler gives an error of 'Unrecognized escape sequence', but if we write the code like this:

str2 = @"\\MyServer\TestFolder\NewFolder";

the compiler does not generate an error.

Another example:

String str1 = “Hi\nPlease see attachment in this email.\nThanks,\njohn”

You can use this string as:

String Str1 = @“Hi Please see attachment in this email Thanks, John”

You can directly use keywords or data types like string, int, char in our scripts or use the .net class method like Int32 for integers and String (capital S) for strings. Specifying this way means, you are directly using .net class called Int32 of type class. But if you use int, it is of type struct. Hover over your mouse to Int32 number = 10; and int no = 10;

If you use .net classes directly, you have to specify the namespace called system, on the top of your script. Like, using System;

Using string and String, and Int32 and int are all same.

Last updated