Compress spaces to one space

Andrej Mitrovic andrej.mitrovich at gmail.com
Tue Feb 21 09:16:20 PST 2012


On 2/21/12, bearophile <bearophileHUGS at lycos.com> wrote:
> Andrej Mitrovic:
>
>> Is there a Phobos function to compress all spaces to just one space in a
>> string?
>>
>> E.g. " foo   bar             "
>> becomes: " foo bar "
>
> import std.string;
> void main() {
>     assert(" foo   bar             ".squeeze() == " fo bar ");
> }

Yikes! I didn't even notice it squeezes *all* duplicates. You see in
my original code I just wanted extra spaces removed, not the chars
themselves. So the right call is:

squeeze(" ")


More information about the Digitalmars-d-learn mailing list