现在在list控件中有若干选项
比如
ppv1_11
pp2_33
PP3_44
P3_99
PP6_22
等等
怎样提取“_”前面的字符(但不包含最后一个数字),不重复,如提取出PPV,PP,P

解决方案 »

  1.   

    一、用Mid函数依次取出每个字符,可以找到数字的位置,然后用Left函数取出字符串。
    二、把取得的字符串添加到一个集合中。添加前要检查这个串是否存在。
    最后集合中的就是不重复的内容。
      

  2.   

    这样比较:  
    dim str as string
    str="P3_99"
    dim code as long
    dim i as long
    for i=1 to len(str)
    code=ascw(mid(str,i,1))
    if code>=48 and code<=57 then
    '数字
    exit for
    end if
    next
      

  3.   

    form1.list1数据照原文:
    ppv1_11 
    pp2_33 
    PP3_44 
    P3_99 
    PP6_22 下面的方法不用数组,减少数组循环查找重复费时。暂时不是很明确楼主的目的,整了两套方案1.Private Sub Command1_Click()
    Dim ss$, s$(), i&
    ss = ","
    For i = 0 To Form1.List1.ListCount - 1
         If 0 < InStr(Form1.List1.List(i), "_") Then
            s = Split(Form1.List1.List(i), "_")
            If 0 = InStr(UCase(ss), UCase("," & Left(s(0), Len(s(0)) - 1) & ",")) Then
                ss = ss & Left(s(0), Len(s(0)) - 1) & "," '固定取消最后一位
            End If
         End If
    Next i
    MsgBox Mid(ss, 2)
    End Sub
    2.Private Sub Command1_Click()
    Dim ss$, s$(), i&
    ss = ","
    For i = 0 To Form1.List1.ListCount - 1
         If 0 < InStr(Form1.List1.List(i), "_") Then
            s = Split(Form1.List1.List(i), "_")
            For j = 1 To Len(s(0))
                If IsNumeric(Mid(s(0), j, 1)) = True Then
                    If 0 = InStr(UCase(ss), UCase("," & Left(s(0), j - 1) & ",")) Then
                        ss = ss & Left(s(0), Len(s(0)) - 1) & "," '固定取数字前
                    End If
                    Exit For
                End If
            Next j
         End If
    Next i
    MsgBox Mid(ss, 2)
    End Sub
      

  4.   

    可能没表达清楚,先谢谢这位兄弟,
    只需要把PPV,PP,P这几个字符放到一个数组就可以了,不是要这样按逗号来排列
      

  5.   

    你再用下zz=split(xx,",")不就行了吗?
      

  6.   

    可以简化为取每项的Left(str,Len(str)-4),然后去重。
      

  7.   


    ss数组里面就是你要的东西,最后那句 MsgBox Mid(ss, 2) 不要就可以了。
      

  8.   

    9l看错了。你把 MsgBox Mid(ss, 2) 这句修改一下,换成下面这句:
    StrSS=split(Mid(ss, 2),“,”)
    StrSS数组就是你要的东西。
      

  9.   


    Option ExplicitPrivate Sub Command1_Click()
        
        Dim i As Long
        Dim tmp As String
        Dim col As Collection
        Dim Result() As String
        
        Set col = New Collection
        On Error Resume Next
        For i = 0 To List1.ListCount - 1
            tmp = Mid(List1.List(i), 1, InStr(List1.List(i), "_") - 1)
            '如果只要去除最后一位数字(多位数字也是去一位)
            If Right(tmp, 1) Like "[0-9]" Then Mid(tmp, Len(tmp), 1) = Chr(32)
            '检验重复
            col.Add UCase(Trim(tmp)), UCase(Trim(tmp))   '不区分大小写
        Next
        On Error GoTo 0
        '结果放入数组
        ReDim Result(col.Count - 1)
        For i = 0 To col.Count - 1
            Result(i) = col(i + 1)
        Next
        '打印数组内容
        Debug.Print Join(Result)
        
    End SubPrivate Sub Form_Load()
        
        List1.AddItem "ppv1_11"
        List1.AddItem "pp2_33"
        List1.AddItem "PP3_44"
        List1.AddItem "P3_99"
        List1.AddItem "PP6_22"
        
    End Sub
      

  10.   


    Private Sub Command1_Click()
        Dim i As Integer, j As Integer
        Dim flg As Boolean, fag As Boolean
        Dim jg() As String, tmp As String
        ReDim jg(0)
        
        For i = 0 To List1.ListCount - 1
            tmp = List1.List(i)
            tmp = Left(tmp, InStr(tmp, "_") - 2)
            If Not fag Then
               jg(0) = tmp
               fag = True
            Else
               For j = 0 To UBound(jg)
                   If UCase(tmp) = UCase(jg(j)) Then '不分大小写,分的话去掉ucase
                      flg = True
                      Exit For
                   End If
               Next
               If Not flg Then
                  ReDim Preserve jg(UBound(jg) + 1)
                  jg(UBound(jg)) = tmp
               Else
                  flg = False
               End If        End If
        Next
    End SubPrivate Sub Form_Load()
        List1.AddItem "ppv1_11": List1.AddItem "pp2_33"
        List1.AddItem "PP3_44": List1.AddItem "P3_99"
        List1.AddItem "PP6_22"
    End Sub
      

  11.   

    了解一下Left$,Mid$()等函数的用法