Search⌘ K
AI Features

Working with Text: Advanced String Operations

Explore advanced string handling techniques in C# to join, format, and build strings efficiently. Understand the benefits of using StringBuilder over simple concatenation to optimize memory and performance in your .NET applications.

Let’s see the joining, formatting, and other string members and learn how to build strings efficiently.

Joining, formatting, and other string members

There are many other string members, as shown in the following table:

Member

Description

Trim, TrimStart, TrimEnd

These methods trim whitespace characters such as space, tab, and carriage return from the beginning and/or end.

ToUpper, ToLower

These convert all the characters into uppercase or lowercase.

Insert, Remove

These methods insert or remove some text.

Replace

This replaces some text with other text.

string.Empty

This can be used instead of allocating memory each time you use a literal string value using an empty pair of double quotes ("").

string.Concat

This concatenates two string variables. The + operator does the equivalent when used between string operands.

string.Join

This concatenates one or more string variables with a character in between each one.

string.IsNullOrEmpty

This checks whether a string variable is null or empty.

string.IsNullOrWhitespace

This checks whether a string variable is null or whitespace; that is, a mix of any number of horizontal and vertical spacing characters, for example, tab, space, carriage return, line feed, and so on.

string.Format

An alternative method to string interpolation for outputting formatted string values, which uses positioned instead of named parameters.

Note: Some of the preceding methods are ...