On Sunday, 24 January 2016 at 20:56:20 UTC, Jon D wrote:
I'm trying to identify the preferred ways to lower case a string. In std.uni there are two functions that return the lower case form of a string: toLower() and asLowerCase(). There is also toLowerInPlace().

toLower will allocate a new string, leaving the original untouched.

toLowerInPlace will modify the existing string.

asLowerCase will returned the modified data as you iterate over it, but will not actually allocate the new string.


toLower is convenient if you need to store a copy of the string somewhere or pass it to a function that expects an existing string, but allocating the new one means it is the slowest of the three.

toLowerInPlace is only usable if your buffer is writable, which many strings aren't, and might still allocate once. It is a middle ground for use in a relatively rare case, but if you are building a string and need to store it somewhere, this is a decent choice.

asLowerCase is the only one that will never actually build a new string, and thus typically gives best performance, but at the cost of a little bit of lesser convenience. If you are just going to loop over it though (including passing it to further transforming algorithms), use this! Or if you want to manage the allocation of the new string yourself, you can use this too.




As a general rule, the asLowerCase (etc.) version should be your first go since it is the most efficient. But the others are around for convenience in cases where you need a new string built anyway.

Reply via email to