Thread overview | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
October 20, 2014 Removing whitespace duplicates | ||||
---|---|---|---|---|
| ||||
How can I extend string line = "car wash"; line.strip.splitter!isWhite.joiner("_").to!string so that car wash becomes car_wash and not car___wash ? Used at https://github.com/nordlow/justd/blob/master/knet.d#L1142 |
October 20, 2014 Re: Removing whitespace duplicates | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nordlöw | On Monday, 20 October 2014 at 20:27:19 UTC, Nordlöw wrote: > line.strip.splitter!isWhite.joiner("_").to!string Doh, that was too easy: line.strip.splitter!isWhite.filter!(a => !a.empty).joiner(lineSeparator).to!S; Sorry, for being lazy ;) |
October 20, 2014 Re: Removing whitespace duplicates | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nordlöw | Nordlöw:
> How can I extend
>
> string line = "car wash";
>
> line.strip.splitter!isWhite.joiner("_").to!string
>
> so that
>
> car wash
>
> becomes
>
> car_wash
>
> and not
>
> car___wash
>
> ?
Use std.string.tr.
Bye,
bearophile
|
October 20, 2014 Re: Removing whitespace duplicates | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | On Mon, 20 Oct 2014 22:21:09 +0000, bearophile wrote:
> Use std.string.tr.
>
> Bye,
> bearophile
std.string.squeeze might be more appropriate.
|
October 20, 2014 Re: Removing whitespace duplicates | ||||
---|---|---|---|---|
| ||||
Posted in reply to Justin Whear | Justin Whear:
> std.string.squeeze might be more appropriate.
But with tr you can also replace the spaces with the underscore.
Bye,
bearophile
|
October 21, 2014 Re: Removing whitespace duplicates | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | On Monday, 20 October 2014 at 23:25:18 UTC, bearophile wrote:
> But with tr you can also replace the spaces with the underscore.
std.string.tr is my preferred choice here :)
Thanks!
|
October 21, 2014 Re: Removing whitespace duplicates | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nordlöw | On Tuesday, 21 October 2014 at 07:31:59 UTC, Nordlöw wrote:
> std.string.tr is my preferred choice here :)
It would be nice to have a lambda-variant of std.string.tr to call like
x.tr!isWhite(['_'])
|
October 21, 2014 Re: Removing whitespace duplicates | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nordlöw | Nordlöw:
> It would be nice to have a lambda-variant of std.string.tr to call like
>
> x.tr!isWhite(['_'])
If your text is ASCII, then there is std.ascii.whitespace that can be given as argument to std.string.tr.
Bye,
bearophile
|
October 22, 2014 Re: Removing whitespace duplicates | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | On Tuesday, 21 October 2014 at 08:04:13 UTC, bearophile wrote: > If your text is ASCII, then there is std.ascii.whitespace that can be given as argument to std.string.tr. > > Bye, > bearophile http://dlang.org/phobos/std_ascii.html#.whitespace is of string but std.string.tr needs [string] as argument. How does that work? |
October 22, 2014 Re: Removing whitespace duplicates | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nordlöw | On Wednesday, 22 October 2014 at 19:00:36 UTC, Nordlöw wrote: > On Tuesday, 21 October 2014 at 08:04:13 UTC, bearophile wrote: >> If your text is ASCII, then there is std.ascii.whitespace that can be given as argument to std.string.tr. >> >> Bye, >> bearophile > > http://dlang.org/phobos/std_ascii.html#.whitespace > > is of string but > > std.string.tr needs [string] as argument. > > How does that work? string a = "test \t \t test"; writeln(a.tr(std.ascii.whitespace,"_","s")); |
Copyright © 1999-2021 by the D Language Foundation