StringBuilderExtensionReplaceWhiteSpaceWith(StringBuilder, ReadOnlySpan`1Char, Int32, Boolean) Method

Replaces in a section of builder, which starts at startIndex and extends to the end of builder, all sequences of white space with replacement.

Definition

Namespace: FolkerKinzel.Strings
Assembly: FolkerKinzel.Strings (in FolkerKinzel.Strings.dll) Version: 9.4.0+10a7d4d71aa960998e32ac0ac6c4fcbe4164c917
C#
public static StringBuilder ReplaceWhiteSpaceWith(
	this StringBuilder builder,
	ReadOnlySpan<char> replacement,
	int startIndex,
	bool skipNewLines = false
)

Parameters

builder  StringBuilder
The StringBuilder whose content is changed.
replacement  ReadOnlySpanChar
A read-only character span that is the replacement for all white space sequences. If an empty span is passed to the parameter, each white space will be completely removed.
startIndex  Int32
The zero-based index in builder at which the replacement starts.
skipNewLines  Boolean  (Optional)
Pass true to exclude newline characters from the replacement. The default value is false.

Return Value

StringBuilder
A reference to builder.

Usage Note

In Visual Basic and C#, you can call this method as an instance method on any object of type StringBuilder. When you use instance method syntax to call this method, omit the first parameter. For more information, see Extension Methods (Visual Basic) or Extension Methods (C# Programming Guide).

Remarks

The method uses IsWhiteSpace(Char) to identify white space characters and works more thoroughly with it than Regex.Replace(string input, @"\s+", string replacement).

IsNewLine(Char) is used to identify newline characters.

Exceptions

ArgumentNullExceptionbuilder is null.
ArgumentOutOfRangeExceptionstartIndex is less than zero or greater than the number of characters in builder.

See Also