我的情况是,我有一个seq[char]
,像这样:
import sequtils
var s: seq[char] = toSeq("abc".items)
转换s
回字符串(即"abc"
)的最佳方法是什么?Stringifying with$
似乎给"@[a, b, c]"
,这不是我想要的。
我的情况是,我有一个seq[char]
,像这样:
import sequtils
var s: seq[char] = toSeq("abc".items)
转换s
回字符串(即"abc"
)的最佳方法是什么?Stringifying with$
似乎给"@[a, b, c]"
,这不是我想要的。
最有效的方法是编写自己的过程。
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)
import sequtils, strutils
var s: seq[char] = toSeq("abc".items)
echo(s.mapIt(string, $it).join)
Join 仅适用于seq[string]
,因此您必须先将其映射到字符串。
您也可以尝试使用演员表:
var s: seq[char] = @['A', 'b', 'C']
var t: string = cast[string](s)
# below to show that everything (also resizing) still works:
echo t
t.add('d')
doAssert t.len == 4
echo t
for x in 1..100:
t.add('x')
echo t.len
echo t