Strings & Characters
Double Quotes
Strings can only use double quotes.
Single quotes can only be used with individual/single characters.
string myString = "All strings must be in double quotes";
char myChar = 's';
System.Char
The System.Char data type is a value data type.
A char represents a single UNICODE character and occuppies two bytes.
The value of a char object is a 16-bit numerical value.
A character literal is specified within single quotes.
System.Char myChar = 'A';
System.String
The System.String data type is a reference data type.
This is a read-only collections of char objects.
The length property returns the number of char objects that it contains.
A string literal is specified within double quotes
System.String myString = "some text";
string myString2 = "some text";
Declaring
All Strings should be initialised when they are declared.
If string variables are not initialised they are given the value null by default.
System.String myString1;
System.String myString1 = null;
string myString2;
string myString2 = null;
Initialising
All strings should be initialised before they are used.
The empty string is an instance of the System.String object that contains zero characters.
System.String myString1 = System.String.Empty;
string myString2 = string.Empty;
System.Environment.NewLine
Common Escape Sequences
| \' | single quote - needed for character literals |
| \" | double quote - used to print a double quote character, needed for string literals |
| \\ | backslash - used to print a backslash character |
| \n | newline - position the cursor at the beginning of the next line |
| \t | horizontal tab - move the cursor to the next tab stop |
| \r | carriage return - positions the cursor at the beginning of the current line |
| \v | vertical quote |
| \a | Alert |
| \b | Backspace |
| \f | form feed |
Empty vs ""
Since .NET 2.0 the following two lines are equivalent.
System.String myString1 = "";
System.String myString2 = System.String.Empty;
@ Prefix
This puts everything within the string declaration "as is"
Using a verbatim literal enables you to add newline characters and other special characters to your strings.
To include a speech mark in an verbatim string literal you need to include it twice.
string myString;
myString = @"C:\temp"
System.Diagnostics.Trace.Write(myString); '= "C:\temp"
myString = @"My ""Temp\"" folder"
System.Diagnostics.Trace.Write(myString); '= "My "Temp" folder"
Char Literals
String Literals
A string literal constant is a hard coded string value.
There are two types of string literals in C#.
You have the regular string literal which is enclosed in speech marks.
You also have the verbatim literal which is prefixed with an ampersand character.
Regular String Literals
string myString;
myString = "C:/temp"
System.Diagnostics.Trace.Write(myString); '= "C:/temp"
myString = "C://temp"
System.Diagnostics.Trace.Write(myString); '= "C://temp"
Some characters cannot be included directly into a string, for example a double quotation mark.
Instead of using just these characters you will have to prefix them with an escape sequence.
myString = "C:\\temp"
System.Diagnostics.Trace.Write(myString); '= "C:\temp"
myString = "C:\"temp"
System.Diagnostics.Trace.Write(myString); '= "C:"temp"
Handles fixed length strings and do not need editing
The Chars property holds an array of all the characters in a string starting at index 0.
Hard code a string literal
If you want to hard code a string literal to get the length
"Details (" & sMyId & ")"
= Substring(LEN("Details"))
System.String.Copy
There are a couple of ways you can copy strings.
string sName;
sName = System.String.Copy("firstname")
String.Equals
sName.Equals(sAnotherString)
Equal Character (=)
string sName;
sName = "firstname"
sName = sADifferentString
Null
You should only assign the keyword null to a string when you want to represent an unassigned object.
The null keyword is the default value when a String variables has not been initialised.
Any attempt to call a method on a null string will generate a NullReferenceException.
Checking for Null
There are a couple of ways you can check for a null value.
System.String myString = null;
if (myString == null) { }
if (System.String.IsNullOrEmpty(myString)) { } // added in .NET 3.5
if (System.String.IsNullOrWhiteSpace(myString)) { } // added in .NET 4.0
Padding Strings
Select Case Variable & Microsoft.VisualBasic.Space(20 - Variable.Length)
Case "-----------20 chars-------"
Case "MyVariable---------------"
End Select
© 2026 Better Solutions Limited. All Rights Reserved. © 2026 Better Solutions Limited TopNext