首页  ·  知识 ·  编程语言
VB.NET含有双引号CSV每行数据分割
林自创的专栏  http://blog.csdn.net/linaren/  VB  编辑:dezai  图片来源:网络
主要解决CSV字段数据带有双引号的问题 具体代码如下: view plaincopy to clipboardprint?Public Function ChangeCsvSplitLine(ByVal s

主要解决CSV字段数据带有双引号的问题

具体代码如下:

view plaincopy to clipboardprint?
Public Function ChangeCsvSplitLine(ByVal strLine As String, ByVal iColNumber As Integer) As String()  
 
    Dim strList() As String = strLine.Split(",")  
 
    If strList.Length = iColNumber Then 
 
        Return strList  
 
    End If 
 
 
 
 
 
    Dim i As Integer = 0  
 
    Dim findSplitIndex As Integer = -1  
 
    Dim index As Integer = 0  
 
    Dim returnList(iColNumber) As String 
 
    Dim strMerger As String = "" 
 
 
 
    For i = 0 To strList.Length - 1  
 
        If findSplitIndex = -1 Then 
 
            If (strList(i)(0) = """" And strList(i)(strList(i).Length - 1) <> """") _  
 
               Or (strList(i).Length = 1 And strList(i) = """") Then 
 
                findSplitIndex = i  
 
            Else 
 
                returnList(index) = strList(i)  
 
                index = index + 1  
 
            End If 
 
        Else 
 
            If (strList(i)(0) <> """" And strList(i)(strList(i).Length - 1) = """") _  
 
               Or (strList(i).Length = 1 And strList(i) = """") Then 
 
                strMerger = "" 
 
                For findSplitIndex = findSplitIndex To i  
 
                    strMerger = strMerger & strList(findSplitIndex) & "," 
 
                Next 
 
                strMerger = strMerger.Substring(0, strMerger.Length - 1)  
 
                returnList(index) = strMerger  
 
                index = index + 1  
 
                findSplitIndex = -1  
 
            End If 
 
        End If 
 
    Next 
 
    Return returnList  
 
End Function 

    Public Function ChangeCsvSplitLine(ByVal strLine As String, ByVal iColNumber As Integer) As String()

        Dim strList() As String = strLine.Split(",")

        If strList.Length = iColNumber Then

            Return strList

        End If

 

 

        Dim i As Integer = 0

        Dim findSplitIndex As Integer = -1

        Dim index As Integer = 0

        Dim returnList(iColNumber) As String

        Dim strMerger As String = ""

 

        For i = 0 To strList.Length - 1

            If findSplitIndex = -1 Then

                If (strList(i)(0) = """" And strList(i)(strList(i).Length - 1) <> """") _

                   Or (strList(i).Length = 1 And strList(i) = """") Then

                    findSplitIndex = i

                Else

                    returnList(index) = strList(i)

                    index = index + 1

                End If

            Else

                If (strList(i)(0) <> """" And strList(i)(strList(i).Length - 1) = """") _

                   Or (strList(i).Length = 1 And strList(i) = """") Then

                    strMerger = ""

                    For findSplitIndex = findSplitIndex To i

                        strMerger = strMerger & strList(findSplitIndex) & ","

                    Next

                    strMerger = strMerger.Substring(0, strMerger.Length - 1)

                    returnList(index) = strMerger

                    index = index + 1

                    findSplitIndex = -1

                End If

            End If

        Next

        Return returnList

    End Function

 

本文作者:林自创的专栏 来源:http://blog.csdn.net/linaren/
CIO之家 www.ciozj.com 微信公众号:imciow
   
免责声明:本站转载此文章旨在分享信息,不代表对其内容的完全认同。文章来源已尽可能注明,若涉及版权问题,请及时与我们联系,我们将积极配合处理。同时,我们无法对文章内容的真实性、准确性及完整性进行完全保证,对于因文章内容而产生的任何后果,本账号不承担法律责任。转载仅出于传播目的,读者应自行对内容进行核实与判断。请谨慎参考文章信息,一切责任由读者自行承担。
延伸阅读