VB.Net传递字符串列表作为字符串.format的参数

编程入门 行业动态 更新时间:2024-10-27 01:29:01
本文介绍了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 ),数据( 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是对的: 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的参数

本文发布于:2023-11-24 08:28:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1624474.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   参数   列表   VB   Net

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!