Quantcast
Channel: C# Archives - Steve Gordon - Code with Steve
Viewing all articles
Browse latest Browse all 17

String Manipulation in C#: Best Practices

0
0

Last week my sixth Pluralsight course, “String Manipulation in C#: Best Practices” was released. This new course dives into creating, modifying, searching and parsing strings in .NET. Strings are one of the most used types in .NET applications and In this course, you’ll uncover everything you need to know about strings to use them effectively in your applications.

The course is designed to provide useful content to developers of all levels. Beginners of C# will learn the fundamentals of using the string and char types. More experienced developers will learn about key implementation details and advanced string manipulation techniques. Along the way, I explain the current best practices for each area of string manipulation so that viewers can easily apply them in their projects.

Module Breakdown

The String and Char Types

This module serves as an introduction to the string and char types in .NET. This is crucial content for new .NET developers but includes implementation details which I expect will be interesting and useful to all developers. I cover the important concept of immutability which is extremely important to understand when manipulating strings. The demos in this module show how strings and chars can be initialised.

Clips in this module:

  • Introducing Strings
  • String Implementation Details
  • Declaring and Initializing Strings
  • Immutability of Strings
  • Introducing Chars
  • Declaring and Initializing Chars
  • Strings and Characters
  • Char Static Methods
  • String and Char Best Practices

Comparing Strings

Once you begin using strings in your applications, comparing them will be an extremely common operation. This module describes the conceptual differences between comparison and equality in .NET types and demonstrates how to apply both in practice. When you are interested in determining equality or sort order, this content will help you apply the best practices to your operations. I also discuss the key role that culture plays when comparing strings.

Clips in this module:

  • Comparison and Equality
  • Variables Affecting Comparisons
  • Comparing Strings
  • Sorting Strings
  • Cultural Differences When Sorting
  • String Equality
  • Cultural Differences for Equality
  • The Equality Operators
  • Comparing to Null or Empty
  • Roslyn Analyzers
  • Best Practices for String Comparisons

Searching Strings

Another extremely common operation, once you use strings in .NET, is searching within those strings for partial or complete matches. This module describes some of the most useful techniques and considerations that you should apply when searching strings. It covers finding content within strings, locating the position of the content and applying regular expressions to match patterns within strings. For advanced developers, it touches on how the new Span type in .NET can be used to search within string data efficiently.

Clips in this module:

  • Searching Strings
  • Finding Content within Strings
  • Locating Text within Strings
  • Finding Text Patterns Using Regular Expressions
  • Searching with ReadOnlySpan

Formatting Types as Strings

It’s often useful to format values from types representing data such as numbers and dates into their string representation. This often happens when persisting and logging data. This module describes techniques for formatting the common types, including how to control the way that the data is represented.

Clips in this module:

  • Introducing String Formatting
  • Formatting Numbers
  • Formatting Dates and Times
  • Formatting Enums and GUIDs

Concatenating and Formatting Strings

Once you have strings in your applications, you will frequently need to combine them for output and persistence. In this module, I demonstrate techniques for joining, concatenating and formatting strings. Viewers will learn how to use composite formatting and to control the final structure of the produced strings. I also demonstrate how to use interpolated strings as a very convenient way to format simple string data.

Clips in this module:

  • Concatenating Strings Using Operators
  • Composite Formatting of Strings
  • Format String Component
  • Alignment Component
  • Concatenating and Joining Collections of Strings
  • Inserting Text into Strings Using String Interpolation

Efficient String Manipulation

Building on the topics from the previous module, the clips in this module focus on how developers can be more efficient when manipulating strings, avoiding the potential pitfalls that string immutability introduces. The primary focus will be learning about the StringBuilder type, demonstrating its use and explaining how it works. Later, the demos show some advanced low and zero overhead techniques for manipulating strings, benchmarking them to compare the performance gains they can offer.

Clips in this module:

  • Getting Started with StringBuilder
  • StringBuilder Implementation Details
  • Configuring a StringBuilder
  • Working with StringBuilders
  • When to Use StringBuilder
  • StringBuilder Performance Considerations
  • Creating Strings with Zero Overhead

Modifying Strings

This module focuses on modifying strings with techniques for splitting strings, extracting substring, and dealing with case and whitespace. The demos include examples of how regular expressions can assist with more complex matching requirements.

Clips in this module:

  • Splitting Strings
  • Splitting Strings with Regex
  • Extracting Substrings
  • Changing Case
  • Dealing with Whitespace
  • Replacing Text Using Regex

Parsing Strings

Earlier in the course, we learned how to format .NET types such as numerics and dates into their string representations. This module describes techniques for reversing this operation to parse instances of these types from string data. It discusses the important role that culture plays in such operations and how to control how data is interpreted during parsing.

Clips in this module:

  • Parsing Numbers
  • Parsing Booleans
  • Parsing Dates and Times
  • Parsing Enums and Chars

String Encoding in .NET

The final module in the course expands on the topic of encoding and how it affects the way that characters are represented. We learn about the Unicode standard and how its code points are represented in .NET, including how larger code points representing emojis are supported through surrogate pairs. The demos show how string data can be encoded into different forms for transmission over networks. The module concludes by explaining advanced concepts such as runes and grapheme clusters.

Clips in this module:

  • Introducing Encoding
  • Unicode Code Points
  • UTF-8
  • Encoding Strings as ASCII and UTF-8
  • Runes and Grapheme Clusters

Summary

In all, the course is 3 hours and 24 minutes in length, split over 10 modules for easy bitesize viewing. It took me over 180 hours to research, script, record and edit the final content. Strings are a core concept in .NET, which all developers must master. This course will teach you everything you need to know about the best practices for working with strings using C#.

I really hope that many developers find it helpful in their day-to-day work and that the distilled best practices help improve their code. Please let me know how you get on with the course. You can watch the course today with your Pluralsight subscription.

If you haven’t already, you can check out my previous courses:

Of course, you can also follow me directly on Pluralsight to get notified when I release new content.

The post String Manipulation in C#: Best Practices appeared first on Steve Gordon - Code with Steve.


String Manipulation in C#: Best Practices was first posted on May 10, 2021 at 12:58 pm.
©2017 "Steve Gordon's Blog". Use of this feed is for personal non-commercial use only. If you are not reading this article in your feed reader, then the site is guilty of copyright infringement. Please contact me at sgordon@hotmail.co.uk

Viewing all articles
Browse latest Browse all 17

Latest Images

Trending Articles





Latest Images