Thread overview | ||||||
---|---|---|---|---|---|---|
|
February 21, 2012 Compress spaces to one space | ||||
---|---|---|---|---|
| ||||
Is there a Phobos function to compress all spaces to just one space in a string? E.g. " foo bar " becomes: " foo bar " |
February 21, 2012 Re: Compress spaces to one space | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrej Mitrovic | 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 ");
}
Bye,
bearophile
|
February 21, 2012 Re: Compress spaces to one space | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | Oh cool, it even takes an optional parameter. Thanks!
On 2/21/12, bearophile <bearophileHUGS@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 ");
> }
>
> Bye,
> bearophile
>
|
February 21, 2012 Re: Compress spaces to one space | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | On 2/21/12, bearophile <bearophileHUGS@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(" ")
|
Copyright © 1999-2021 by the D Language Foundation