从Integers数组构建VB.NET字符串

问题描述 投票:0回答:2

我需要根据.csv文件中的Unicode值构造一个字符串。我已经使用Python来提取整数值,我需要在VB.NET中重建字符串,以检查字符串是否存在于文件解析器中。我已经尝试使用ChrChrW函数,但是当我达到8315值时,两者都给我错误。

Public Function FromAsciiArray(AsciiArray As Integer()) As String

    Dim strOut As String = ""

    For Each asciiValue In AsciiArray
        strOut += ChrW(asciiValue)
    Next
    Return strOut

End Function

Dim strVECTOR = FromAsciiArray(
    New Integer() {86, 69, 67, 84, 79, 82, 32, 40, 109, 109, 47, 115, 8315, 185, 41}
)

有人知道怎么做吗?

string vb.net unicode
2个回答
2
投票

这有效:

Dim arr = New Integer() {86, 69, 67, 84, 79, 82, 32, 40, 109, 109, 47, 115, 8315, 185, 41}
Dim str = String.Concat(arr.Select(Function(n) Convert.ToChar(n)))
Console.WriteLine(str)

1
投票
Public Function FromIntegers(CharacterValues As IEnumerable(Of Integer)) As String
    Return New String(CharacterValues.Select(Function(c) Convert.ToChar(c)).ToArray())    
End Function

这将与数组,列表和其他整数序列一起使用(从名称中删除“数组”,并在我们处理时修复“ASCII”)。您可以像在问题中一样调用它:

Dim VECTOR As String = FromIntegers(
    New Integer() {86, 69, 67, 84, 79, 82, 32, 40, 109, 109, 47, 115, 8315, 185, 41}
)

...我做了一点调整,现代代码倾向于使用定义而不是名称前缀来声明变量的类型。

如果你真的想玩得开心,你也可以这样做:

Public Function FromIntegers(ParamArray CharacterValues() As Integer) As String
    Return New String(CharacterValues.Select(Function(c) Convert.ToChar(c)).ToArray())    
End Function

这将让你像在问题中一样调用函数:

Dim VECTOR As String = FromIntegers(
    New Integer() {86, 69, 67, 84, 79, 82, 32, 40, 109, 109, 47, 115, 8315, 185, 41}
)

并且也称它为:

Dim VECTOR As String = FromIntegers(86, 69, 67, 84, 79, 82, 32, 40, 109, 109, 47, 115, 8315, 185, 41)

最后,如果您确定字符编码并且取决于Python端如何处理事物或如何解析csv数据,我们仍然可以通过System.Text.Encoding类系列做得更好。

© www.soinside.com 2019 - 2024. All rights reserved.