vlang

Convert a string to an array in vlang


I'm learning V, and as far as my attempts go, although a V string is an array of bytes, array methods are not applicable to strings. So I want to convert a string to an array. I have tried searching for this with no success, I found something in Go, but it's not usable in V:

[]byte("Here is a string....")

Is there any way to convert a string to an array in V?


Solution

  • You can use string.split(delim string) to just array and string.bytes() for an array of bytes:

    Welcome to the V REPL (for help with V itself, type `exit`, then run `v help`).
    V 0.2.2 8650ec6
    Use Ctrl-C or `exit` to exit, or `help` to see other available commands
    >>> x := "test"
    >>> x.split("")
    ['t', 'e', 's', 't']
    >>> x.bytes()
    [t, e, s, t]