Need help: Return reference slice

advibm via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Oct 29 13:02:02 PDT 2014


On Wednesday, 29 October 2014 at 19:54:45 UTC, bearophile wrote:
> advibm:
>
>> I would like to have something like that:
>>
>> char[] buf; // already filled array
>> char[] partOfBuf = betweenTwoStrings(buf, "START", "END");
>> partOfBuf[0] = 'a'; // THIS should also change the 'buf' 
>> variable
>> assert(buf[0] == 'a');
>>
>> Thanks for your help
>
> To do this you don't need to return a ref slice. A slice 
> suffices. But D strings/wstrings/dstrings are made of immutable 
> chars, so you can't modify them. So you need to work with 
> char[] or dchar[] or wchar[].
>
> Bye,
> bearophile

Thank you for your fast answer.
I am embarrased but my code already works as expected.
I made a mistake when I wanted to change the partOfBuf variable.
I wrote: partOfBuf = replacedArray;
But I had to write partOfBuf[0..$] = replacedArray.


More information about the Digitalmars-d-learn mailing list