Converting a seq[char] to string

Sp3000

I'm in a situation where I have a seq[char], like so:

import sequtils
var s: seq[char] = toSeq("abc".items)

What's the best way to convert s back into a string (i.e. "abc")? Stringifying with $ seems to give "@[a, b, c]", which is not what I want.

Reimer Behrends

The most efficient way is to write a procedure of your own.

import sequtils
var s = toSeq("abc".items)

proc toString(str: seq[char]): string =
  result = newStringOfCap(len(str))
  for ch in str:
    add(result, ch)

echo toString(s)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related