分享

自定义函数

 *蓝星* 2019-07-20

 '【读文本文件到字典 Dictionary】

    Public Function Txt2Dict() As Integer

        Dim cKey As String = vbNull

        Dim cReadLine As String = vbNull

        Dim cPath As String = Application.StartupPath & "\Gpdmk.ini"

        Dim nVal As Integer = 0

        Try

            cDict.Clear()

            Dim SR As StreamReader = New StreamReader(cPath, System.Text.ASCIIEncoding.Default)

            Do While SR.Peek() >= 0

                cReadLine = SR.ReadLine().ToString.Trim

                If InStr(cReadLine, "=") > 0 Then

                    cKey = Mid(cReadLine, 1, InStr(cReadLine, "=") - 1).ToString

                    If Not cDict.ContainsKey(cKey) Then

                        cDict.Add(cKey, (Mid(cReadLine, InStr(cReadLine, "=") + 1)).ToString)

                    End If

                End If

            Loop

            SR.Close()

            If cDict.Count > 0 Then nVal = 1

        Catch ex As Exception

            MsgBox(ex.ToString, 48, "错误信息")

        End Try

        Return nVal

    End Function

    '【读文本文件到集合 List】

    Public Function Txt2List() As Integer

        Dim cReadLine As String = vbNull

        Dim cPath As String = Application.StartupPath & "\GpdmkA.txt"

        Dim nVal As Integer = 0

        Try

            cList.Clear()

            Dim SR As StreamReader = New StreamReader(cPath, System.Text.ASCIIEncoding.Default)

            Do While SR.Peek() >= 0

                cReadLine = SR.ReadLine().ToString.Trim

                cList.Add(cReadLine)

            Loop

            SR.Close()

            If cList.Count > 0 Then nVal = 1

        Catch ex As Exception

            MsgBox(ex.ToString, 48, "错误信息")

        End Try

        Return nVal

    End Function

    '【读文本文件到数组】

    Public Function Txt2Array() As Integer

        Dim cPath As String = Application.StartupPath & "\GpdmkA.txt"

        Dim nVal As Integer = 0

        Try

            Dim ReadText As String = My.Computer.FileSystem.ReadAllText(cPath, System.Text.ASCIIEncoding.Default)

            cArray = Split(ReadText, vbCrLf)

            If UBound(cArray) > 0 Then nVal = 1

        Catch ex As Exception

            MsgBox(ex.ToString, 48, "错误信息")

        End Try

        Return nVal

    End Function

    '【字符串判断文字类型】

    Public Function IsWzlxs(WzsIn As String, Optional ByVal nCs As Integer = 0) As String

        Dim cLx As String = Nothing

        Dim cLxStr As String = Nothing

        Dim cZf As String = Nothing

        Dim cLxOut As String = Nothing

        Try

            For i As Integer = 1 To WzsIn.Length

                cZf = Mid(WzsIn, i, 1)

                If IsNumeric(cZf) Then

                    cLx = "N"       '数字

                ElseIf cZf.Length = System.Text.Encoding.Default.GetByteCount(cZf) Then

                    cLx = "E"       '英文字母

                ElseIf cZf.Length <> System.Text.Encoding.Default.GetByteCount(cZf) Then

                    cLx = "C"       '汉字字符

                End If

                If InStr(cLxStr, cLx) = 0 Then cLxStr = cLxStr & cLx

                If cLxStr.Length > 2 Then Exit For

            Next

            cLxOut = IIf(cLxStr.Length = 1, cLxStr, IIf(nCs = 1, cLxStr & " B", "B"))    '"B" 混合

        Catch ex As Exception

            MsgBox(ex.ToString, 48, "错误信息")

        End Try

        Return cLxOut

    End Function

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多