First N runes of a string?

I have a $string that I need to shorten. {{ $string | first 42 }} works reasonably well, as long as the strings is ASCII, which is rarely the case for me, so I usually end up with $string cut half-rune and looking ugly.

Is there a way Iโ€™m missing to get first N runes of a string?

{{ $str := "ไธ–็•Œไธ–็•Œไธ–็•Œ. Mixing it up. ๐Ÿ™‚ ไธ–็•Œไธ–็•Œไธ–็•Œ, ไธ–็•Œ" }}
RuneCount: {{ strings.RuneCount $str  }}
First 23 Runes: {{ substr $str 0 23}}

That ends with a smile for me. Wonderful when things just work the way they should.

While this is a guess, many modern programming languages donโ€™t equate strings to arrays precisely for the reason youโ€™re seeing. first is a command for arrays, probably safer to use the ones meant for strings.


This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.