求一字符串分割函数。 作用:将一字符串按指定位数分割,分割方式同microsoft office的word软件,遇到不同类型的数据会分开处理,若数据相同则按指定长度分割。比如“汉中工349803中”,分割长度为2,则分割后为“汉中”、“工”、“349803”、“中”。若遇到“()”,则“()”里的内容单独分割

解决方案 »

  1.   

    在C#中 分割是用到Split()返回的是一个数组!
      

  2.   

    字符串必須有特定的組合才行,如果隨意的,比如“漢中&*330((ddd))^^(&^&&)   DAF)"這樣的字符串,如何分割?
      

  3.   


    说个思路吧。1 首先用 Replace() 函数将串中的 ")" 全部替换成 "("。然后用 Split(strSource, "(") 将初始串分割成子串。2 循环遍历各个子串。遇到符合上述分隔规则的分界点,或直接切割,或在串中插入分割符(如"(")最后 Split。
      

  4.   

    Option ExplicitEnum CharType
        ctSpliter
        ctAnsi
        ctUnicode
    End EnumSub Main()
        Dim aWords()    As String
        aWords = SplitWords("汉中工349803中", 2)
        Debug.Print Join(aWords, vbCrLf)
    End SubSub ArrayAdd(a() As String, Count As Long, ByVal Value As String)
        Count = Count + 1
        ReDim Preserve a(Count - 1)
        a(Count - 1) = Value
    End SubFunction SplitWords(ByVal Text As String, ByVal UniWordLen As Long) As String()
        Dim aWords()    As String
        Dim lWordCount  As Long
        Dim ch          As String
        Dim sWord       As String
        Dim eLastType   As CharType
        Dim eNewType    As CharType
        Dim i           As Long    For i = 1 To Len(Text)
            ch = Mid$(Text, i, 1)
            If (AscW(ch) And &HFF00) = 0 Then
                Select Case ch
                    Case "(", ")"
                        eNewType = ctSpliter
                    Case Else
                        eNewType = ctAnsi
                End Select
            Else
                eNewType = ctUnicode
            End If
            If eNewType <> eLastType Then
                If eLastType <> ctSpliter Then
                    If Len(sWord) <> 0 Then
                        ArrayAdd aWords, lWordCount, sWord
                    End If
                End If            If eNewType = ctSpliter Then
                    sWord = vbNullString
                Else
                    sWord = ch
                End If
                eLastType = eNewType
            Else
                If eLastType <> ctSpliter Then
                    sWord = sWord & ch
                    If (eLastType = ctUnicode) And (Len(sWord) = UniWordLen) Then
                        ArrayAdd aWords, lWordCount, sWord                    sWord = vbNullString
                    End If
                End If
            End If
        Next    If eLastType <> ctSpliter Then
            If Len(sWord) <> 0 Then
                ArrayAdd aWords, lWordCount, sWord
            End If
        End If    SplitWords = aWords
    End Function
      

  5.   

    Private Sub Command3_Click()
        Dim strSource As String
        Dim lngPos As Long: lngPos = 1
        Dim strNumber As String
        
        strSource = "汉中工349803中(某人某事某时间111某天)什么啊这是"
        Do While lngPos <= Len(strSource)
            If WhatChar(Mid(strSource, lngPos, 1)) = 0 Then '汉字
                If lngPos + 1 <= Len(strSource) Then
                    If WhatChar(Mid(strSource, lngPos + 1, 1)) = 0 Then
                        Debug.Print Mid(strSource, lngPos, 2)
                        lngPos = lngPos + 2
                    Else
                        Debug.Print Mid(strSource, lngPos, 1)
                        lngPos = lngPos + 1
                    End If
                Else
                    Debug.Print Mid(strSource, lngPos, 1)
                    lngPos = lngPos + 1
                End If
            ElseIf WhatChar(Mid(strSource, lngPos, 1)) = 2 Then '字母或符号
                Debug.Print Mid(strSource, lngPos, 1)
                lngPos = lngPos + 1
            Else '数字
                strNumber = ""
                Do
                    strNumber = strNumber & Mid(strSource, lngPos, 1)
                    lngPos = lngPos + 1
                Loop Until WhatChar(Mid(strSource, lngPos, 1)) <> 1
                Debug.Print strNumber
            End If
        Loop
    End Sub
    Private Function WhatChar(ByVal vStr As String) As Integer
        Dim gbascii As Byte
        Dim intChar As Integer
        If Asc(vStr) < 0 Then
            gbascii = AscB(StrConv(vStr, vbFromUnicode))
            '区位码在16区之后的为汉字
            If gbascii - 160 > 15 Then
                intChar = 0 ' "是汉字"
            Else
                intChar = 2 ' "是全角符号"
            End If
        Else
            intChar = 1 ' "是半角英文或数字"
        End If
        WhatChar = intChar
    End Function