Where did my string go in D

jessekphillips

Jesse Phillips

Posted on August 26, 2019

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/

💖 💪 🙅 🚩
jessekphillips
Jesse Phillips

Posted on August 26, 2019

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Memory Free Programming in D
dlang Memory Free Programming in D

February 11, 2020

Regular Expressions in D
regex Regular Expressions in D

December 30, 2019

Printing in D
dlang Printing in D

December 15, 2019

List Comprehension in D
dlang List Comprehension in D

December 13, 2019

Compile-Time Reflection
dlang Compile-Time Reflection

December 9, 2019