www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - lower case only first letter of word

reply Marc <jckj33 gmail.com> writes:
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
next sibling parent ketmar <ketmar ketmar.no-ip.org> writes:
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
prev sibling next sibling parent Daniel Kozak <kozzi11 gmail.com> writes:
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
prev sibling next sibling parent reply Daniel Kozak <kozzi11 gmail.com> writes:
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
parent reply Marc <jckj33 gmail.com> writes:
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:

 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)
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
parent reply Mengu <mengukagan gmail.com> writes:
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:
 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)
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
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; }
Dec 05 2017
next sibling parent reply Mengu <mengukagan gmail.com> writes:
On Tuesday, 5 December 2017 at 14:34:57 UTC, Mengu wrote:
 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:
 [...]
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
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; }
however a solution that does not allocate any memory would be a lot better.
Dec 05 2017
parent reply Steven Schveighoffer <schveiguy yahoo.com> writes:
On 12/5/17 10:00 AM, Mengu wrote:
 On Tuesday, 5 December 2017 at 14:34:57 UTC, Mengu wrote:
 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:
 [...]
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
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; }
however a solution that does not allocate any memory would be a lot better.
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. -Steve
Dec 05 2017
next sibling parent =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
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.
 
 -Steve
One 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
prev sibling parent reply kdevel <kdevel vogtner.de> writes:
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
next sibling parent =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
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
prev sibling parent Steven Schveighoffer <schveiguy yahoo.com> writes:
On 12/5/17 2:41 PM, kdevel wrote:
 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).
Define foo as: foo(R)(R r) if (isInputRange!R && isSomeChar!(ElementType!R)) Then it can take any range of char types.
 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
prev sibling parent Jacob Carlborg <doob me.com> writes:
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
prev sibling parent codephantom <me noyb.com> writes:
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