#String indexing

1 messages · Page 1 of 1 (latest)

vast cargo
#

I looked at the standard library documentation and did not see anything about indexing strings ?
I wanted to make my own but I need to get the tail of the string (equivalent of rest in [_, ..rest] for a list) but I'm not sure how to do so (except string.slice(s, 1, string.length(s)-1))

Is there a way to index a string (for example to get the 7th character of a string) ?
If not, is there a way to get the tail of the string ?

valid kernel
#

you can do both with string.slice, or convert it to graphemes with string.to_graphemes and operate on the list

vast cargo
#

Doesn't string.slice make a whole new copy of the string ?

valid kernel
#

on the Erlang target string.slice uses Erlang's string:slice which seems to return a substring -> a copy is not created

vast cargo
#

Thanks

#

I'm happy not to worry about memory

median wolf
#

Strong indexing is slow in any language with proper Unicode support btw

#

You generally don’t want to use it

vast cargo
#

So in order to traverse a string, would it be preferable to use string.slice or convert it to a list of chars and traverse it ?