代码之家  ›  专栏  ›  技术社区  ›  sab669

如果我使用数据可视化工具而不是观察值,为什么我的字符串只包含换行符?

  •  1
  • sab669  · 技术社区  · 7 年前

    我的应用程序应该执行以下操作:

    • 接受输入文件,每行包含1条记录
    • 验证记录
    • 删除所有无效记录
    • 将剩余数据传递到数据库

    我遇到了一个奇怪的问题,我的文件包含多行,如果我在Visual Studio中使用数据可视化工具,则该字符串包含多行,但当我尝试将 string.split i n to a n array(spliting on \r\n )的结果存储时,我只在数组中获取一个元素。这是我的手表标签的屏幕截图:

    第一行是my filecontents variable,a string

    如果我使用文本可视化器,你可以看到它被分解成不同的行。如果我将数据复制并粘贴到记事本中,我们可以看到回车和换行。

    下面的行是 filedata array,它使用 string.split填充

    这是实际代码:

    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()。
    
    端函数
    < /代码> 
    
    

    因此,我遇到的问题是我的forloop在此行引发了一个异常: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)我的数组中只有一个元素。以下是我的手表标签的屏幕截图:

    enter image description here

    第一排是我的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;
    }
    

    我做错了什么?

    1 回复  |  直到 7 年前
        1
  •  3
  •   the_lotus    7 年前

        Dim s1 As String = vbCrLf
        Dim s2 As String = Environment.NewLine
        Dim s3 As String = Chr(13) & Chr(10)
    

    Private Function GetFileData(ByVal strBaseDir As String) As List(Of String)
        If (Not System.IO.File.Exists(strBaseDir)) Then
            GetFileData = String.Empty
            Return Nothing
        End If
    
        Dim lines As new List(Of String)
    
        For Each line As String In System.IO.File.ReadAllLines(strBaseDir)
            Dim elements As String() = line.Split(",")
    
            If (Not elements.Length = 15) Then
                lines.Clear()
                lines.Add("BadCommaCount")
                Return lines
            End If
    
            lines.Add(line)
        Next
    
        Return lines
    End Function