Skip to content

About

Hands-on C# strings and text — immutability, StringBuilder measured, culture traps, StringComparison, regex with timeouts. Part 09 of a practice series.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Latest commit

 

History

2 Commits

Folders and files

Repository files navigation

C# Strings & Text — Hands-on Examples

Part 09 of a hands-on C# practice series. Five runnable, heavily commented examples: immutability and interning proven with ReferenceEquals, the +=-in-a-loop disaster measured against StringBuilder, culture formatting traps (the "3,50" → 350 silent corruption, date ambiguity), correct comparing (StringComparison, the Turkish-i bug reproduced live), and regex essentials with [GeneratedRegex] and ReDoS timeouts.

Running

dotnet run        # all examples
dotnet run 3      # just one (e.g. culture traps)

What's inside

# Topic Highlights
01 Immutability methods return new strings, interning, why == compares content
02 StringBuilder 20k appends measured, the API tour, when plain strings win
03 Formatting & culture specifiers, alignment, "3,50" parsed as 350 in en-US, ISO dates
04 Comparing StringComparison everywhere, Turkish-i reproduced, ordinal vs culture sort
05 Regex [GeneratedRegex], named groups, MatchEvaluator, ReDoS timeout demo

The rules to remember

  1. Machines get InvariantCulture; humans get their own culture — never the ambient default.
  2. Every Contains/Equals/sort says how it compares (Ordinal for machine data).
  3. Never ToUpper to compare — Equals(..., OrdinalIgnoreCase).
  4. String grows in a loop → StringBuilder; joining a list → string.Join.
  5. Regex that sees user input gets a timeout; JSON/HTML get real parsers.

Further reading

The series

← Previous: 08 Modern C# 8–12 · Next: 10 Files & JSON →

Part Topic
01 Collections
02 LINQ
03 Delegates & events
04 Async/await
05 Generics
06 Types & memory
07 Exceptions
08 Modern C# 8–12
09 Strings & text (this repo)
10 Files & JSON
11 MiniShop microservices (finale)

Author

Matej Zlatunić

License

Released under the MIT License. Feel free to use it for learning.

About

Hands-on C# strings and text — immutability, StringBuilder measured, culture traps, StringComparison, regex with timeouts. Part 09 of a practice series.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages