image

C# Tutorial 10 | Characters and Strings - C# Tutorials

12 Jan 2024

CHARACTERS AND STRINGS

CHARACTERS

The Character type is represented using two bytes and uses the Unicode standard (https://home.unicode.org/). A char can only be 1 character. Single quotes are used to specify a char. For example

char newChar = 'b';

ESCAPE SEQUENCES

Escape sequences are characters that have a special meaning and will be interpreted differently than ‘standard’ characters. They take the form of a slash followed by a character.

QUOTE ESCAPE SEQUENCE

Single quotes and double quotes are the characters used to define the bounds of a character and string respectively. You might genuinely want to use one of these characters within the string. To do this you can put a backwards slash ahead of this character and it will no longer be recognised as the end of a char or string. For example

char newChar = '''; Console.WriteLine(newChar);

BACKSLASH ESCAPE SEQUENCE

The same logic can be applied to the backslash character itself, you might want to use the character instead of it being interpreted as the escape character. To do this just put another backwards slash in front of it.

char newChar = '\'; Console.WriteLine(newChar);

NULL ESCAPE SEQUENCE

To represent the null character, you would do the following:

char newChar = '\0'; Console.WriteLine(newChar);

ALERT ESCAPE SEQUENCE

The alert or bell character is:

char newChar = '\a'; Console.WriteLine(newChar);

BACKSPACE ESCAPE SEQUENCE

The backspace character is:

char newChar = '\b'; Console.WriteLine(newChar);

FORM FEED ESCAPE SEQUENCE

The Form feed character is:

char newChar = '\f'; Console.WriteLine(newChar);

NEW LINE ESCAPE SEQUENCE

The new line character is (this one is pretty common):

char newChar = '\n'; Console.WriteLine(newChar);

CARRIAGE RETURN ESCAPE SEQUENCE

The Carriage Return character is:

char newChar = '\r'; Console.WriteLine(newChar);

HORIZONTAL TAB ESCAPE SEQUENCE

The Horizontal Tab character is:

char newChar = '\t'; Console.WriteLine(newChar);

VERTICAL TAB ESCAPE SEQUENCE

The Vertical Tab character is:

char newChar = '\v'; Console.WriteLine(newChar);

CHAR TO NUMERIC CONVERSION

As chars in C# can also be represented by their ASCII values, it is possible to convert a char into a numeric type of at least unsigned short.

If the type is smaller than an unsigned short then an explicit conversion will need to occur.

Character to numeric

THE STRING TYPE

A string is an immutable array of characters, unlike a char, strings are specified within double-quotes. For example:

string newString = "This is a new string!"; Console.WriteLine(newString); Although string is a reference type, the standard equality operator will function like that of a value type. This means that ” == ” can be used to compare the value stored by the reference type.

The list of escape sequences we explored earlier also work within strings.

VERBATIM STRING LITERALS

A verbatim string literal is a string that does not recognise escape sequences and therefore the backslash character can be used without issue. This is very useful when you come to specify file locations.

To use a verbatim string literal you place a ” @ ” character before the opening double quote of your string. For example

string verbatimString = @"\This \is \a \verbatim \string \literal. All of the backwards slashes are intact."; Console.WriteLine(verbatimString);

CONCATENATING STRINGS

The word concatenate means to link two or more things together. This is no different in the case of string concatenation – the process of linking strings together.

The ‘+’ operator can be used in conjunction with a number of strings to add them together. For example:

string a = "Hello"; string b = ", "; string c = "World!"; Console.WriteLine(a + b + c); //Outputs "Hello, World!" However, using this operator to build large strings is ineffective and should be avoided.

STRING INTERPOLATION

Another approach to linking strings is interpolation.

Any string that is preceded with a dollar sign $ is referred to as an interpolated string – these strings can utilise other values within curly braces { }.

string toBeIncluded = "once"; string inclusion = $"This sentence will display {toBeIncluded}"; Console.WriteLine(inclusion); int x = 1; string y = $"This sentence will display {x} time"; Console.WriteLine(y); As you can see above, any type can be used in string interpolation. Behind the scenes, C# calls ToString() on these values to convert them to a string.

Unless the string is also a verbatim string, the interpolated string must only span one line otherwise it will not work.