DEV Community

Jesse Phillips
Jesse Phillips

Posted on

2 1

Where did my string go in D

I didn't search this one for Python, after my last post I thought it important to call out an unexpected behavior when using std.algorithms with strings.

Remember the grapheme note, well in the original range design for string, it was thought that a 32bit UTF character was a complete visual character. Grapheme this was not the case.

This results in going from char[] to a range of dchar. While this works for continuing with the algorithm, generally by the end you want a string again.

import std.range;
import std.conv;

string s = "foo".take(2).to!string;
Enter fullscreen mode Exit fullscreen mode

As an aside, the examples I've given throughout this tutorial have all been written on my phone without a compiler. In general it should be possible to just copy them into main and it will work, except when calling unimplemented function. Apologies if they don't compile.

void main () {
} 
Enter fullscreen mode Exit fullscreen mode

https://run.dlang.io/

AWS GenAI LIVE image

How is generative AI increasing efficiency?

Join AWS GenAI LIVE! to find out how gen AI is reshaping productivity, streamlining processes, and driving innovation.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more