digitalmars.D.learn - lower case only first letter of word
- Marc (3/3) Dec 05 2017 Does D have a native function to capitalize only the first letter
- ketmar (6/9) Dec 05 2017 http://dpldocs.info/experimental-docs/std.string.capitalize.html
- Daniel Kozak (3/6) Dec 05 2017 Something like this: https://dlang.org/phobos/std_uni.html#asCapitalized
- Daniel Kozak (4/11) Dec 05 2017 but this will change all other uppercase to lowercase, so maybe it is no...
- Marc (5/19) Dec 05 2017 Yes, this is not what I want. I want to convert only the first
- Mengu (10/32) Dec 05 2017 this is how i'd do it:
- Mengu (3/22) Dec 05 2017 however a solution that does not allocate any memory would be a
- Steven Schveighoffer (19/43) Dec 05 2017 Non-allocating version:
- =?UTF-8?Q?Ali_=c3=87ehreli?= (17/39) Dec 05 2017 One using existing facilities:
- kdevel (9/25) Dec 05 2017 On Tuesday, 5 December 2017 at 17:25:57 UTC, Steven Schveighoffer
- =?UTF-8?Q?Ali_=c3=87ehreli?= (35/38) Dec 05 2017 That's how ranges work. LowerCaseFirst produces dchar elements one at a
- Steven Schveighoffer (7/37) Dec 05 2017 Define foo as:
- Jacob Carlborg (4/15) Dec 05 2017 That's not Unicode aware and is only safe to do with single byte charact...
- codephantom (17/20) Dec 06 2017 // ----------------
Does D have a native function to capitalize only the first letter of the word? (I'm asking that so I might avoid reinvent the wheel, which I did sometimes in D)
Dec 05 2017
Marc wrote:Does D have a native function to capitalize only the first letter of the word? (I'm asking that so I might avoid reinvent the wheel, which I did sometimes in D)http://dpldocs.info/experimental-docs/std.string.capitalize.html http://dpldocs.info/experimental-docs/std.uni.asCapitalized.html searching rox! p.s.: but beware, it will lowercase all the letters except the first one, so it may not be exactly the thing you aksed for.
Dec 05 2017
Something like this: https://dlang.org/phobos/std_uni.html#asCapitalized On Tue, Dec 5, 2017 at 2:31 PM, Marc via Digitalmars-d-learn < digitalmars-d-learn puremagic.com> wrote:Does D have a native function to capitalize only the first letter of the word? (I'm asking that so I might avoid reinvent the wheel, which I did sometimes in D)
Dec 05 2017
but this will change all other uppercase to lowercase, so maybe it is not what you want. If you really want just change first char to upper, then there is nothing wrong to do it yourself On Tue, Dec 5, 2017 at 2:37 PM, Daniel Kozak <kozzi11 gmail.com> wrote:Something like this: https://dlang.org/phobos/std_uni.html#asCapitalized On Tue, Dec 5, 2017 at 2:31 PM, Marc via Digitalmars-d-learn < digitalmars-d-learn puremagic.com> wrote:Does D have a native function to capitalize only the first letter of the word? (I'm asking that so I might avoid reinvent the wheel, which I did sometimes in D)
Dec 05 2017
On Tuesday, 5 December 2017 at 13:40:08 UTC, Daniel Kozak wrote:but this will change all other uppercase to lowercase, so maybe it is not what you want. If you really want just change first char to upper, then there is nothing wrong to do it yourself On Tue, Dec 5, 2017 at 2:37 PM, Daniel Kozak <kozzi11 gmail.com> wrote:Yes, this is not what I want. I want to convert only the first letter of the word to lower case and left all the others immutable. similar to PHP's lcfirst(): http://php.net/manual/en/function.lcfirst.phpSomething like this: https://dlang.org/phobos/std_uni.html#asCapitalized On Tue, Dec 5, 2017 at 2:31 PM, Marc via Digitalmars-d-learn < digitalmars-d-learn puremagic.com> wrote:Does D have a native function to capitalize only the first letter of the word? (I'm asking that so I might avoid reinvent the wheel, which I did sometimes in D)
Dec 05 2017
On Tuesday, 5 December 2017 at 14:01:35 UTC, Marc wrote:On Tuesday, 5 December 2017 at 13:40:08 UTC, Daniel Kozak wrote:this is how i'd do it: string upcaseFirst(string wut) { import std.ascii : toUpper; import std.array : appender; auto s = appender!string; s ~= wut[0].toUpper; s ~= wut[1..$]; return s.data; }but this will change all other uppercase to lowercase, so maybe it is not what you want. If you really want just change first char to upper, then there is nothing wrong to do it yourself On Tue, Dec 5, 2017 at 2:37 PM, Daniel Kozak <kozzi11 gmail.com> wrote:Yes, this is not what I want. I want to convert only the first letter of the word to lower case and left all the others immutable. similar to PHP's lcfirst(): http://php.net/manual/en/function.lcfirst.phpSomething like this: https://dlang.org/phobos/std_uni.html#asCapitalized On Tue, Dec 5, 2017 at 2:31 PM, Marc via Digitalmars-d-learn < digitalmars-d-learn puremagic.com> wrote:Does D have a native function to capitalize only the first letter of the word? (I'm asking that so I might avoid reinvent the wheel, which I did sometimes in D)
Dec 05 2017
On Tuesday, 5 December 2017 at 14:34:57 UTC, Mengu wrote:On Tuesday, 5 December 2017 at 14:01:35 UTC, Marc wrote:however a solution that does not allocate any memory would be a lot better.On Tuesday, 5 December 2017 at 13:40:08 UTC, Daniel Kozak wrote:this is how i'd do it: string upcaseFirst(string wut) { import std.ascii : toUpper; import std.array : appender; auto s = appender!string; s ~= wut[0].toUpper; s ~= wut[1..$]; return s.data; }Yes, this is not what I want. I want to convert only the first letter of the word to lower case and left all the others immutable. similar to PHP's lcfirst(): http://php.net/manual/en/function.lcfirst.php[...]
Dec 05 2017
On 12/5/17 10:00 AM, Mengu wrote:On Tuesday, 5 December 2017 at 14:34:57 UTC, Mengu wrote:Non-allocating version: 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); } Warning: it ain't going to be fast. Auto-decoding everywhere. -SteveOn Tuesday, 5 December 2017 at 14:01:35 UTC, Marc wrote:however a solution that does not allocate any memory would be a lot better.On Tuesday, 5 December 2017 at 13:40:08 UTC, Daniel Kozak wrote:this is how i'd do it: string upcaseFirst(string wut) { import std.ascii : toUpper; import std.array : appender; auto s = appender!string; s ~= wut[0].toUpper; s ~= wut[1..$]; return s.data; }Yes, this is not what I want. I want to convert only the first letter of the word to lower case and left all the others immutable. similar to PHP's lcfirst(): http://php.net/manual/en/function.lcfirst.php[...]
Dec 05 2017
On 12/05/2017 09:25 AM, Steven Schveighoffer wrote:Non-allocating version: 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); } Warning: it ain't going to be fast. Auto-decoding everywhere. -SteveOne using existing facilities: import std.range; import std.uni; import std.algorithm; auto lowerCaseFirst(R)(R r) { R rest = r.save; rest.popFront(); return chain(r.front.toLower.only, rest); } unittest { assert(lowerCaseFirst("SchveiGoffer").equal("schveiGoffer")); assert(lowerCaseFirst("ŞchveıĞöffer").equal("şchveıĞöffer")); } void main() { } Ali
Dec 05 2017
On Tuesday, 5 December 2017 at 17:25:57 UTC, Steven Schveighoffer wrote: [...]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); } Warning: it ain't going to be fast. Auto-decoding everywhere.But one cannot use the return value of lowerCaseFirst as argument for foo(string). Only the use as argument to writeln seems to work. Also I had to put import std.range.primitives : front, popFront, empty; outside the struct otherwise the compiler complains about missing front, popFront and empty for type string.
Dec 05 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
Dec 05 2017
On 12/5/17 2:41 PM, kdevel wrote:On Tuesday, 5 December 2017 at 17:25:57 UTC, Steven Schveighoffer wrote: [...]Define foo as: foo(R)(R r) if (isInputRange!R && isSomeChar!(ElementType!R)) Then it can take any range of char types.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); } Warning: it ain't going to be fast. Auto-decoding everywhere.But one cannot use the return value of lowerCaseFirst as argument for foo(string).Only the use as argument to writeln seems to work. Also I had to put import std.range.primitives : front, popFront, empty; outside the struct otherwise the compiler complains about missing front, popFront and empty for type string.Yeah, it wasn't a complete example. These are the extensions to arrays that allow them to work as ranges. -Steve
Dec 05 2017
On 2017-12-05 15:34, Mengu wrote:this is how i'd do it: string upcaseFirst(string wut) { import std.ascii : toUpper; import std.array : appender; auto s = appender!string; s ~= wut[0].toUpper; s ~= wut[1..$]; return s.data; }That's not Unicode aware and is only safe to do with single byte characters. -- /Jacob Carlborg
Dec 05 2017
On Tuesday, 5 December 2017 at 13:31:17 UTC, Marc wrote:Does D have a native function to capitalize only the first letter of the word? (I'm asking that so I might avoid reinvent the wheel, which I did sometimes in D)// ---------------- module test; import std.stdio; void main() { string myString = "heLlo WoRlD!"; writeln( HereItIs(myString) ); } string HereItIs(string someString) { import std.uni : toLower; import std.ascii : toUpper; return (someString.ptr[0].toUpper ~ someString[1..$].toLower); } // ------------------
Dec 06 2017