String operations and interpolation
A string holds text. Operations such as Replace and Trim return a string result rather than changing the original text. Interpolation inserts values into a readable message using a dollar sign before the opening quote.
string original = " A-12 ";string clean = original.Trim();System.Console.WriteLine($"[{original}]");System.Console.WriteLine($"[{clean}]");Trim returns the text without the surrounding spaces. The original variable still refers to its original text.
Expected output (reasoned, not executed):
[ A-12 ][A-12]Read and construct text
A string variable can be assigned a different value, even though its current string object cannot be edited in place. Use text.Length to count UTF-16 code units, and text[0] to read the first unit only after checking that the string is not empty. Indices begin at zero. For simple ASCII shelf codes, each letter occupies one unit.
Select the operation that fits
Trim() removes leading and trailing whitespace; Replace(oldValue, newValue) replaces occurrences of a substring. Substring(startIndex, length) selects a contiguous portion; its second argument is a count, not the ending index. Contains answers whether a substring exists. These methods do not validate the business meaning of a shelf code.
Interpolation and formatting
In $"Shelf {shelf}: {count} books", braces delimit an expression. Use doubled braces for literal braces in an ordinary interpolated string. A format after a colon, such as {count:D3}, changes the textual representation, not the stored number. Numeric formatting can depend on culture; a later lesson makes the provider explicit.
string code = "A-12";string number = code.Substring(2, 2);string moved = code.Replace("A-", "B-");System.Console.WriteLine(number);System.Console.WriteLine(moved);The two units starting at index 2 are the digits. Replacing the prefix produces a new result.
Expected output (reasoned, not executed):
12B-12string shelf = "C";int count = 7;System.Console.WriteLine($"Shelf {shelf}: {count} books");System.Console.WriteLine($"{{{shelf}}}");Interpolation inserts the expression values. The doubled outer braces remain literal.
Expected output (reasoned, not executed):
Shelf C: 7 books{C}Common mistake and correction
Calling code.Replace("A", "B"); without storing or using the result does not update code. Assign the result back to the variable if replacement is intended. Check length before indexing; an empty string is not null, but neither has a readable first character.
Try it yourself
Clean the fixed input " R-08 ", extract 08, and build Shelf R: 08 without changing the original value. Repeat with " R-00 ".
Next: Unicode and text comparison. Examples target C# 14 / net10.0. They are static study material; compiler and ByteMind execution have not been verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
String operations and interpolation — knowledge check
Apply the lesson concepts to the following scenarios.
What does Trim change in the original string object?
What is "A-12".Substring(2, 2)?
Which expression inserts the value of count?
What does $"{{A}}" produce?
Which check prevents indexing an empty non-null string?