我正在读取一个CSV,同时进入一个字符串(),其中包含按此顺序排列的值,如: -
"SSN", "Location", "Name", "Sex" 123456, "IOWA", "Jorge", "M"
但是我需要混淆string()中列的顺序,以便顺序应该是
"SSN", "Name", "Sex" "Location", 123456, "Jorge", "M", "IOWA",
如何在Vb.net中实现它
答案 0 :(得分:1)
只需创建一个字典,稍后可以调用它来构建字符串
Dim dictionary As New Dictionary(Of String, String)
dictionary.Add("SSN", "123456")
dictionary.Add("Name", "Jorge")
dictionary.Add("Sex", "M")
dictionary.Add("Location", "IOWA")
而且稍后这个词典将帮助你附加字符串以附加使用字符串构建器,例子在这里,
http://www.dotnetperls.com/stringbuilder-vbnet
在你添加的每个人之后清除字典对象并重新使用它来接收下一个数据。
答案 1 :(得分:1)
这很快,很脏,但它应该让你走上正轨。当您读取字符串时,可以通过拆分将其放入数组中,然后重新排序。
Dim str1 As String = "'SSN', 'Location', 'Name', 'Sex'"
Dim str2 As String = "123456, 'IOWA', 'Jorge', 'M'"
Dim arr1 As Array = Split(str1, ",")
Dim arr2 As Array = Split(str2, ",")
Dim JumbleStr1 As String = arr1(0) & ", " & arr1(2) & ", " & arr1(3) & ", " & arr1(1)
Dim JumbleStr2 As String = arr2(0) & ", " & arr2(2) & ", " & arr2(3) & ", " & arr2(1)
此代码生成:
JumbleStr1 ='SSN','姓名','性别','位置'
JumbleStr2 = 123456,'Jorge','M','IOWA'