Checking, whether string contains only ascii.

kinke via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Feb 22 13:16:24 PST 2017


On Wednesday, 22 February 2017 at 20:07:34 UTC, Ali Çehreli wrote:
> One more:
>
> bool isAscii(string s) {
>     import std.string : representation;
>     import std.algorithm : canFind;
>     return !s.representation.canFind!(c => c >= 0x80);
> }
>
> unittest {
>     assert(isAscii("hello world"));
>     assert(!isAscii("hellö wörld"));
> }
>
> Ali

One more again as I couldn't believe noone went for 'any' yet:

---
import std.algorithm;
return !s.any!"a > 127"; // code-point level
---


More information about the Digitalmars-d-learn mailing list