Discussion:
Removing whitespace duplicates
"Nordlöw" via Digitalmars-d-learn
2014-10-20 20:27:18 UTC
Permalink
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
"Nordlöw" via Digitalmars-d-learn
2014-10-20 20:29:34 UTC
Permalink
Post by "Nordlöw" via Digitalmars-d-learn
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 ;)
bearophile via Digitalmars-d-learn
2014-10-20 22:21:09 UTC
Permalink
Post by "Nordlöw" via Digitalmars-d-learn
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
Justin Whear via Digitalmars-d-learn
2014-10-20 22:29:54 UTC
Permalink
Post by bearophile via Digitalmars-d-learn
Use std.string.tr.
Bye,
bearophile
std.string.squeeze might be more appropriate.
bearophile via Digitalmars-d-learn
2014-10-20 23:25:17 UTC
Permalink
Post by Justin Whear via Digitalmars-d-learn
std.string.squeeze might be more appropriate.
But with tr you can also replace the spaces with the underscore.

Bye,
bearophile
"Nordlöw" via Digitalmars-d-learn
2014-10-21 07:31:58 UTC
Permalink
Post by bearophile via Digitalmars-d-learn
But with tr you can also replace the spaces with the underscore.
std.string.tr is my preferred choice here :)

Thanks!
"Nordlöw" via Digitalmars-d-learn
2014-10-21 07:37:15 UTC
Permalink
Post by "Nordlöw" via Digitalmars-d-learn
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(['_'])
bearophile via Digitalmars-d-learn
2014-10-21 08:04:12 UTC
Permalink
Post by "Nordlöw" via Digitalmars-d-learn
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
"Nordlöw" via Digitalmars-d-learn
2014-10-22 19:00:35 UTC
Permalink
Post by bearophile via Digitalmars-d-learn
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?
via Digitalmars-d-learn
2014-10-22 19:19:45 UTC
Permalink
Post by "Nordlöw" via Digitalmars-d-learn
Post by bearophile via Digitalmars-d-learn
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"));

Continue reading on narkive:
Search results for 'Removing whitespace duplicates' (Questions and Answers)
5
replies
I need some JavaScript help!!?
started 2007-11-07 08:41:16 UTC
programming & design
Loading...