4.1.1.7 - Strings, character codes and dates (C#)

4.1.1.7 - Strings, character codes and dates (C#)

Text often arrives as a single string even when a program needs separate fields, numbers or dates. Learn to locate and extract text, convert character codes, and make numeric and date conversions with explicit formats.

C# route status: incomplete: runnable support pending. Examples and answers are written C# with no Run control; complete onscreen Paper 1 preparation still requires runnable support.

Indexing, substring and concatenation

String operations let a program examine parts of text while preserving the distinction between characters and whole strings.

For the ASCII-only examples here, string code = "AB-204"; has code.Length equal to 6. Indices start at zero: code[0] is the character 'A' and code[5] is '4'. C# string length counts UTF-16 code units; some Unicode symbols occupy two, so it is not universally the number of visible symbols.

code.IndexOf('-') finds the first matching position, here 2. A missing character returns -1; do not use that as a valid substring index. code.Substring(3, 3) returns "204": the first argument is a start index and the second is a length, not an end index. code.Substring(3) returns all text from index 3 onwards. An invalid index or excessive length causes an exception.

Concatenation joins strings: "Order " + code produces "Order AB-204". Strings are immutable. code = code + "X"; assigns a new string; it does not modify the old text in place. To replace a character, construct new text, rather than trying code[0] = 'Z';.

C#
string entry = "Mira:42";
int separator = entry.IndexOf(':');
string name = entry.Substring(0, separator);
string numberText = entry.Substring(separator + 1);
string label = name + " has " + numberText + " points";

Assuming exactly one colon with nonempty fields, separator is 4, name is "Mira" and numberText is "42". separator + 1 skips the delimiter itself. If the input format is not guaranteed, check for the delimiter before extracting; defensive input is taught later.

Character-code conversions

A character code is a number assigned by an encoding system, so a character can be converted to its numerical representation.

C# char stores one UTF-16 code unit. For the basic Latin letters used here, its Unicode number agrees with ASCII: 'A' is 65, 'B' is 66 and 'a' is 97. This does not mean all Unicode text fits into one char per visible symbol.

C#
char letter = 'D';
int characterCode = (int)letter;
char nextLetter = (char)(characterCode + 1);

characterCode is 68 and nextLetter is 'E'. (int) converts the character code unit to an integer; (char) converts a valid code-unit value back. Converting character '7' to int gives its code 55, not the numerical quantity 7. Parsing string "7" as a number is a different operation.

When arithmetic represents a limited alphabet, constrain the range yourself. Adding one to 'Z' gives the next code unit, '[', rather than automatically wrapping to 'A'. A requirement to wrap alphabetically needs explicit selection or remainder logic. A numeric character code also needs to be within the permitted code-unit range for the intended conversion; these tasks use guaranteed basic Latin input.

Continue this lesson.

You’re previewing NovaLearn without an account. Create a free account to read the rest.

Create free account