Task to throw away string parts, use joiner and splitter not very successful
Dfr
deflexor at yandex.ru
Wed Jan 1 09:45:19 PST 2014
Thank you, very clear explained.
On Wednesday, 1 January 2014 at 16:31:43 UTC, Rémy Mouëza wrote:
> You can use `std.conv.to` to convert the dchar[] back to a
> string, adding `.to!string` at the end of the dchar[] array you
> want to convert.
>
> Also not that there exists two similar functions to the lazy
> evaluated splitter() and joiner() in std.array: the eagerly
> evaluated split() and join(); being eager make the code looks
> more straightforward (no `.array` or `.to!string`).
>
> import std.stdio;
> import std.array;
> import std.algorithm;
>
> void eager () {
> string name = "thiš.ìs.à.çtriñg";
> auto parts = name.split (".");
> parts.popBack;
> auto cut = parts.join (".");
> cut.writeln;
> }
>
> void lazy_ () {
> string name = "thiš.ìs.à.çtriñg";
> auto parts = name.splitter (".");
> parts.popBack;
> auto cut = parts.joiner (".").array.to!string;
> cut.writeln;
> }
>
> void main () {
> eager ();
> lazy_ ();
> }
>
> The program prints:
> thiš.ìs.à
> thiš.ìs.à
>
> On 01/01/2014 08:40 AM, Dfr wrote:
>> And one more problem here:
>>
>> string name = "test";
>> auto nameparts = splitter(name, '.');
>> writeln(typeof(joiner(nameparts, ".").array).stringof);
>>
>> This prints "dchar[]", but i need char[] or string, how to get
>> my
>> 'string' back ?
>>
>>
>> On Tuesday, 31 December 2013 at 20:49:55 UTC, Dfr wrote:
>>> Hello, i have string like "this.is.a.string" and want to
>>> throw away
>>> some parts separated by dots, here is first attempt:
>>>
>>> name = "this.is.a.string"; // <-- want to make "this.is.a"
>>> from this
>>> auto nameparts = splitter(name, '.');
>>> auto name1 = joiner(nameparts[0 .. $-1], '.');
>>>
>>> And got this error: "Error: Result cannot be sliced with []"
>>>
>>> So, kinda fixed it (correct way?):
>>>
>>> name = "this.is.a.string";
>>> auto nameparts = splitter(name, '.').array;
>>> auto name1 = joiner(nameparts[0 .. $-1], '.');
>>>
>>> got this:
>>>
>>> Error: template std.algorithm.joiner does not match any
>>> function
>>> template declaration. Candidates are:
>>> /usr/include/dmd/phobos/std/algorithm.d(2846):
>>> std.algorithm.joiner(RoR, Separator)(RoR r, Separator sep) if
>>> (isInputRange!RoR && isInputRange!(ElementType!RoR) &&
>>> isForwardRange!Separator && is(ElementType!Separator :
>>> ElementType!(ElementType!RoR)))
>>>
>>> Stuck here, thank you for any help.
More information about the Digitalmars-d-learn
mailing list