lower case only first letter of word
Ali Çehreli
acehreli at yahoo.com
Tue Dec 5 20:11:42 UTC 2017
On 12/05/2017 11:41 AM, kdevel wrote:
> On Tuesday, 5 December 2017 at 17:25:57 UTC, Steven Schveighoffer wrote:
> But one cannot use the return value of lowerCaseFirst as argument for
> foo(string). Only the use as argument to writeln seems to work.
That's how ranges work. LowerCaseFirst produces dchar elements one at a
time. An easy way of getting a string out of it is calling
std.conv.text, which converts all those dchars to a series of UTF-8 chars.
Note, .text below is an expensive call because it allocates a new
string. You may want to cache its result first if you need the result
more than once:
auto lowered = lowerCaseFirst("HELLO").text;
foo(lowered);
This works:
import std.range;
struct LowerCaseFirst(R) // if(isSomeString!R)
{
R src;
bool notFirst; // terrible name, but I want default false
dchar front() {
import std.uni: toLower;
return notFirst ? src.front : src.front.toLower;
}
void popFront() { notFirst = true; src.popFront; }
bool empty() { return src.empty; }
}
auto lowerCaseFirst(R)(R r)
{
return LowerCaseFirst!R(r);
}
void foo(string s) {
import std.stdio;
writefln("good old string: %s", s);
}
void main() {
import std.conv;
foo(lowerCaseFirst("HELLO").text);
}
Ali
More information about the Digitalmars-d-learn
mailing list