这是实际代码:
private sub-CmdImport(byval sender as object,byval e as system.eventargs)处理CmdImport.click
dim filecontents as string=getfiledata(pstrbedir&pstrfilename)
dim linestoexclude as list(of int16)=new list(of int16)
mfpscanfile(pstrbedir&pstrfilename,perrorstring,prtferrstring,pstroutput,linestoexclude)
'mfpscanfile在文件上循环并验证记录,将行添加到linestoexclude(如果行不正确)
'这里我们尝试删除不正确的行
'首先,我们将新行上的字符串拆分为一个数组
'然后清除LineStoExclude指定的每一行
dim splitter as string()=\r\n_
dim filedata as string()=filecontents.split(拆分器,stringssplitoptions.removeEmptyEntries)
对于i as int16=0 to linestoexclude.count-1
filedata(linestoexclude(i))=“”
接下来
filecontents=string.join(“\r\n”,filedata)
结束子
private函数getfiledata(byval strbasedir as string)as string
如果(不是system.io.file.exists(strbasedir)),则
getFileData=string.empty
退出函数
结束如果
dim sb as stringbuilder=新建stringbuilder()
对于system.io.file.readalllines(strbasedir)中的每行字符串
将元素变暗为string()=line.split(“,”)
如果(非元素,长度=15),则
getFileData=“badcommaCount”
退出函数
结束如果
附加线
接下来
getfiledata=sb.toString()。
端函数
< /代码>
因此,我遇到的问题是我的for
loop在此行引发了一个异常:filedata(linestoexclude(i))=“”
它引发异常,因为filedata只有1个元素。但是我不明白为什么它只有一个元素。监视窗口将我的字符串显示为一行,但可视化工具显示它有换行符,所以为什么我的拆分不起作用?
此外,我在C语言中使用了几乎完全相同的代码,它可以完美地处理同一个文件:
list<int>linestoexclude=new list<int>();
strbadrecs=扫描文件(strbasedir,ref strrrorstring,ref strrtferrstring,ref stroutput,ref linestoexclude);
//清除不良记录
string[]splitter=\r\n“”
string[]filedata=objdemographicmiport.filedata.split(splitter,stringssplitoptions.removeEmptyEntries);
对于(int i=0;i<linestoexclude.count;i++)
{
filedata[linestoexclude[i]]=string.empty;
}
< /代码>
我做错了什么?
将剩余数据传递到数据库
我遇到了一个奇怪的问题,我的文件包含多行,如果我在Visual Studio中使用数据可视化工具,则字符串包含多行,但当我尝试存储String.Split
成一个数组(拆分\r\n
)我的数组中只有一个元素。以下是我的手表标签的屏幕截图:

第一排是我的fileContents
变量Astring
如果我使用文本可视化器,你可以看到它被分解成不同的行。如果我将这些数据复制并粘贴到记事本中,我们可以看到回车和换行。
下面的那条线是fileData
数组,使用字符串
这是实际代码:
Private Sub cmdImport_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdImport.Click
Dim fileContents As String = GetFileData(pStrBaseDir & pStrFileName)
Dim linesToExclude As List(Of Int16) = New List(Of Int16)
mfpScanFile(pStrBaseDir & pStrFileName, pErrorString, pRtfErrString, pStrOutput, linesToExclude)
'mfpScanFile loops over the file and validates the records, adding the row # to linesToExclude if the row is bad
'Here we attempt to remove the bad rows
'First we split the string on new lines into an array
'Then we clear each line specified by linesToExclude
Dim splitter As String() = {"\r\n"}
Dim fileData As String() = fileContents.Split(splitter, StringSplitOptions.RemoveEmptyEntries)
For i As Int16 = 0 To linesToExclude.Count - 1
fileData(linesToExclude(i)) = ""
Next
fileContents = String.Join("\r\n", fileData)
End Sub
Private Function GetFileData(ByVal strBaseDir As String) As String
If (Not System.IO.File.Exists(strBaseDir)) Then
GetFileData = String.Empty
Exit Function
End If
Dim sb As StringBuilder = New StringBuilder()
For Each line As String In System.IO.File.ReadAllLines(strBaseDir)
Dim elements As String() = line.Split(",")
If (Not elements.Length = 15) Then
GetFileData = "BadCommaCount"
Exit Function
End If
sb.AppendLine(line)
Next
GetFileData = sb.ToString()
End Function
所以我的问题是For
循环在此行上引发异常:fileData(linesToExclude(i)) = ""
它抛出异常是因为菲拉达
只有一个元素。但是为什么它只有一个元素,我不明白。监视窗口将我的字符串显示为一行,但可视化工具显示它有换行符,所以为什么我的拆分不起作用?
此外,我在C语言中使用了几乎完全相同的代码,它可以完美地处理同一个文件:
List<int> linesToExclude = new List<int>();
strBadRecs = ScanFile(strBaseDir, ref strErrorString, ref strRtfErrString, ref strOutput, ref linesToExclude);
// Stripping out bad records
string[] splitter = {"\r\n"};
string[] fileData = objDemographicImport.FileData.Split(splitter, StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < linesToExclude.Count; i++)
{
fileData[linesToExclude[i]] = String.Empty;
}
我做错了什么?