Jump to page: 1 2
Thread overview
lower case only first letter of word
Dec 05, 2017
Marc
Dec 05, 2017
ketmar
Dec 05, 2017
Daniel Kozak
Dec 05, 2017
Daniel Kozak
Dec 05, 2017
Marc
Dec 05, 2017
Mengu
Dec 05, 2017
Mengu
Dec 05, 2017
Ali Çehreli
Dec 05, 2017
kdevel
Dec 05, 2017
Ali Çehreli
Dec 05, 2017
Jacob Carlborg
Dec 06, 2017
codephantom
December 05, 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)
December 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.
December 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)
>


December 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)
>>
>
>


December 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:
>
>> 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
December 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:
>> 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;
}
December 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:
>> 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.
December 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
December 05, 2017
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
December 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.
> 
> -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
« First   ‹ Prev
1 2