sorting a string

Steven Schveighoffer via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Jul 14 08:15:42 PDT 2017


On 7/14/17 11:06 AM, Namal wrote:
> Is there a 'easy' way to sort a string in D like it is possible in 
> Python? Also how can I remove whitespace between characters if I am 
> reading a line from a file and safe it as a string?
> 
>      string[] buffer;
> 
>      foreach (line ; File("test.txt").byLine)
>          buffer ~= line.to!string;

import std.algorithm: filter;
import std.uni: isWhite;

line.filter!(c => !c.isWhite).to!string;

be warned, this is going to be a bit slow, but that's the cost of 
autodecoding.

If you are looking for just removing ascii whitespace, you can do it a 
bit more efficiently, but it's not easy.

About the string sorting, you'd have to be more specific.

-Steve


More information about the Digitalmars-d-learn mailing list