VB.Net传递字符串列表作为字符串.format的参数 [英] VB.Net pass list of string as an argument for string .format

查看:76
本文介绍了VB.Net传递字符串列表作为字符串.format的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个更改CSV文件列顺序的程序。

我使用以下代码



 公开 共享  Sub  ReadCSV()

Dim columnstoselect As 字符串()

Dim csvFile As 字符串 = D:\Projects \\ \\ _MQA_Remap_CSV \Input.csv
Dim lines = File.ReadAllLines(csvFile)

Dim outFile 作为 字符串 = D:\Projects\MQA_Remap_CSV \ Output.csv
Dim data As 字符串()

Dim 格式作为 字符串 = CReadConfig.ReadConfigSource( D:\Projects\MQA_Remap_CSV \ config.csv

Form1.TextBox1.Text = CReadConfig.ReadConfigtarget( D:\Projects\MQA_Remap_CSV \ config.csv)。ToString

使用 fs 作为 StreamWriter(outFile, False
对于 每个 s 作为 字符串 行中

data = s .Split( c)

fs.WriteLine( String .Format(format,CReadConfig.ReadConfigtarget( D:\\ \\ PROjects\MQA_Remap_CSV \ config.csv)。ToArray


下一步
结束 使用





我有这个功能的问题



 CReadConfig.ReadConfigtarget(   D:\Projects\MQA_Remap_CSV \ config.csv





函数l这样的事情



 公共 共享 功能 ReadConfigtarget(filePath  As  字符串作为列表(  字符串
' 将目标或数组行的值作为数组返回
Dim str As String ()
Dim lines = File.ReadAllLines(filePath).Skip( 1
Dim val As String
Dim i A s 字符串 = 0

使用 sr 作为 StreamReader(filePath)

Dim out As 列表( 字符串

For 每个 s 作为 字符串 行中
str = s.Split( c)
val =修剪( data(& ; (str( 1 ) - 1 )& )。ToString
out.Add(val)
下一步

返回 out

结束 使用
结束 函数





这个函数读取一个csv文件,应该返回一个看起来像这样的值列表

数据(0),数据(1)



因此如果我替换此行有效

 fs.WriteLine( String  .Format(format,CReadConfig.ReadConfigtarget(  D:\Projects\MQA_Remap_CSV \ config.csv)。ToArray 





with



 fs.WriteLine( String 。格式(格式,数据( 0 ),数据( 1 ),数据(< span class =code-digit> 2 ))





代码工作并写出数据中的数据( 0),数据(1)等,但是当我使用该函数时,它将它作为文字字符串并在csv文件中写出文本'data(0)',data(1)。



关于如何将函数值作为参数而不是文字文本传递的任何想法?

解决方案

如果它正在编写变量名称作为输出,你可能做的是:

 fs.WriteLine( String  .Format(格式,  data(0),data(1),data(2)))



 fs.WriteLine( String  .Format(  {0},{1},{2},data(0),data(1),dat a(2)))



相反。

但是Carlo是对的:

< pre lang =vb> fs.WriteLine( String .Format(format,CReadConfig.ReadConfigtarget( D:\Projects\MQA_Remap_CSV \ config.csv)。ToArray()))

将完美运行。


I'm trying to write a program which changes the column order of a CSV file.
Im using the following code

Public Shared Sub ReadCSV()

        Dim columnstoselect As String()

        Dim csvFile As String = "D:\Projects\MQA_Remap_CSV\Input.csv"
        Dim lines = File.ReadAllLines(csvFile)

        Dim outFile As String = "D:\Projects\MQA_Remap_CSV\Output.csv"
        Dim data As String()

        Dim format As String = CReadConfig.ReadConfigSource("D:\Projects\MQA_Remap_CSV\config.csv")

        Form1.TextBox1.Text = CReadConfig.ReadConfigtarget("D:\Projects\MQA_Remap_CSV\config.csv").ToString

Using fs As New StreamWriter(outFile, False)
            For Each s As String In lines

                data = s.Split(","c)

                fs.WriteLine(String.Format(format, CReadConfig.ReadConfigtarget("D:\Projects\MQA_Remap_CSV\config.csv").ToArray
                                         )
                            )
            Next
        End Using



I am having a problem with this function

CReadConfig.ReadConfigtarget("D:\Projects\MQA_Remap_CSV\config.csv")



the function looks like this

Public Shared Function ReadConfigtarget(filePath As String) As List(Of String)
    'returns the Value of target or array rows as an array
    Dim str As String()
    Dim lines = File.ReadAllLines(filePath).Skip(1)
    Dim val As String
    Dim i As String = 0

    Using sr As New StreamReader(filePath)

        Dim out As New List(Of String)

        For Each s As String In lines
            str = s.Split(","c)
            val = Trim("data(" & (str(1) - 1) & ") ").ToString
            out.Add(val)
        Next

        Return out

    End Using
End Function



this function reads a csv file and should return a list of values which look like this
data(0), data(1)

so in effect if i replace this line

fs.WriteLine(String.Format(format, CReadConfig.ReadConfigtarget("D:\Projects\MQA_Remap_CSV\config.csv").ToArray



with

fs.WriteLine(String.Format(format, data(0), data(1), data(2))



the code works and writes out the data in data(0) ,data(1) and so on , however when i use the function it is taking it as a literal string and writing out text 'data(0)' , data(1) in the csv file.

Any ideas on how to pass the function values as argument and not literal text?

解决方案

If it's writing the variable names as the output then what you've probably done is:

fs.WriteLine(String.Format(format, "data(0), data(1), data(2)"))

Or

fs.WriteLine(String.Format("{0},{1},{2}, data(0), data(1), data(2)"))


Instead.
But Carlo is right:

fs.WriteLine(String.Format(format, CReadConfig.ReadConfigtarget("D:\Projects\MQA_Remap_CSV\config.csv").ToArray()))

will work perfectly.


这篇关于VB.Net传递字符串列表作为字符串.format的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆