StringBuilderExtensionPolyfillExtensionReplaceWhiteSpaceWith(StringBuilder, String, Int32, Int32, Boolean) Method

Replaces in a section of builder, which starts at startIndex and which is count characters long, 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,
	string? replacement,
	int startIndex,
	int count,
	bool skipNewLines = false
)

Parameters

builder  StringBuilder
The StringBuilder whose content is changed.
replacement  String
A String that is the replacement for all sequences of white space, or null to completely remove all white space.
startIndex  Int32
The zero-based index in builder at which the replacement starts.
count  Int32
The length of the specified section in builder where replacement operations take place.
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.
ArgumentOutOfRangeException

startIndex or count are smaller than zero or larger than the number of characters in builder

- or -

startIndex + count is larger than the number of characters in builder.

See Also