In Dlang, the whitespace includes the following Unicode category:
We use the strip
method present in the std.string
module to remove the whitespace or specific characters from the start and end of the string.
string strip(str, chars, leftChars, rightChars);
This method takes four parameters.
str
: String in which the characters are to be stripped.chars
: String of characters to be stripped from the start and end of source string.leftChars
: String of characters to be stripped from the start of the source string.rightChars
: String of characters to be stripped at the end of the source string.The last three arguments are optional, if that is skipped, by default, the whitespace at the start and end of the string is removed.
This method returns a string value. The returned string is created by stripping the given characters from the start and end of the source string.
The code given below demonstrates how to remove whitespace from the string in Dlang.
import std.stdio;import std.string;void main(){string str = " this is a string ";writefln("The string before stripping: \"%s\"", str);str = strip(str);writefln("The string after stripping: \"%s\"", str);}
stdio
and string
module. The stdio
module is used to print the value to output and the string module contains the strip
method.str
and assign a string with whitespace at the start and end, as a value to the variable. strip
method with the str
as the argument. This method will remove all the whitespace at the start and end of the string.