Strings, Encryption, and Testing
In this chapter
- Declaring, creating, and initializing a string data type.
- Implementing some of the common string handling methods used in C#.
- Introduction to regular expressions.
- Rudimentary encryption using the Caesar Cipher.
- How to dry-run a program using a trace table.
A string in C# is a sequence of characters (char
) stored in memory. While the char
data type is used for a single character, we use the string
data type for a sequence of characters.
As discussed in Chapter 4 ("Using the Console"), each character in .NET is represented using a numeric value from the Unicode standard. Unicode succeeded ASCII as the default for character representation. ASCII uses a single byte to represent common English characters and symbols, but it is too limited for non-English alphabets and additional symbols. Unicode, in .NET, uses a 16-bit code, providing 65,535 different available characters.
Although we could theoretically create an array of char
with a specified length, it would be static and require manual processing for each character. While there are some situations where arrays of char
are necessary, C# provides the String
class, which is used more frequently due to its flexibility and ease of use.
One important aspect of the String
class is that it is immutable, meaning that its contents cannot be altered after it is created. Instead, if we try to change a string, a new string object is created in memory:
Internally, a string is represented as an array of characters, allowing individual access to its elements using indices:
From the syllabus
AQA: String-handling operations in a programming language (3.1.1.7/4.1.1.7)
- Be familiar with and be able to use: length, position, substring, concatenation, character to character code, character code to character, string conversion operations.