StringExtensionPolyfillExtensionReplaceWhiteSpaceWith Method

Generates a String in which all sequences of white space are replaced by replacement.

Definition

Namespace: FolkerKinzel.Strings
Assembly: FolkerKinzel.Strings (in FolkerKinzel.Strings.dll) Version: 9.4.0+10a7d4d71aa960998e32ac0ac6c4fcbe4164c917
C#
public static string ReplaceWhiteSpaceWith(
	this string s,
	string? replacement,
	bool skipNewLines = false
)

Parameters

s  String
The source String.
replacement  String
A String, which replaces the white space, or null to remove all white space.
skipNewLines  Boolean  (Optional)
Pass true to exclude newline characters from the replacement. The default value is false.

Return Value

String
A new String in which all sequences of white space are replaced by replacement. If s doesn't contain a white space character, s is returned.

Usage Note

In Visual Basic and C#, you can call this method as an instance method on any object of type String. 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

See Also