Hi,
What's the D's idiom to split a string into an array of every n chars? (prefer one liner)
Thanks.
Thread overview | |||||
---|---|---|---|---|---|
|
April 13, 2022 one liner to split a string into every n chars? | ||||
---|---|---|---|---|
| ||||
Hi, What's the D's idiom to split a string into an array of every n chars? (prefer one liner) Thanks. |
April 13, 2022 Re: one liner to split a string into every n chars? | ||||
---|---|---|---|---|
| ||||
Posted in reply to mw | One way you can do it: import std.range : chunks; import std.algorithm : map; import std.array : array; import std.conv : text; string[] split = "Hello D".chunks(2).map!(v => v.text).array; writeln(split); foreach(val; "Hello D".chunks(2)) { writeln(val.text); } |
April 13, 2022 Re: one liner to split a string into every n chars? | ||||
---|---|---|---|---|
| ||||
Posted in reply to mw | On 4/13/22 2:26 AM, mw wrote: >Hi, What's the D's idiom to split a string into an array of every n chars? (prefer one liner) str.byChar.chunks(2).array; Your request of "array of every n chars" is somewhat ambiguous. This is an array of chunks (not an array of arrays). -Steve |