[int]
str=100,300,宋体,15如上:如何用把100、300、宋体、15这些内容截取出来。这些内容的长度是不固定的。有可能是:1000,50,楷体_GB2312,5,就是说如何利用“,”这个逗号来截取。一个逗号的时候好弄,取100的话,就从第一个开始,再配合instr函数截取到这个逗号在整个字符串的第一次出现的位置,取300的话,就是逗号出现的位置+1开始一直截取到最后,但同在有几个逗号该怎么办呢?

解决方案 »

  1.   

    dim strA
    dim strB()
    strA = "100,300,宋体,15"
    strB = Split(strA,",")
    Dim N as long
    For N = LBound(strB) To UBound(strB)
        Msgbox strB(N)
    Next N
      

  2.   

    Function GetLeftWords(s As String, ByVal Ch As String) As String
        Dim i As Long
        i = InStr(s, Ch)
        If i > 0 Then
            GetLeftWords = Left(s, i - 1)
            s = Mid(s, i + Len(Ch))
        Else
            GetLeftWords = s
            s = vbNullString
        End If
    End Function
    Private Sub Command1_Click()
        Dim str,str1 as String
       
        str="100,300,宋体,15"
        Do While Len(str) > 0
            str1 = GetLeftWords(FileNames, ",")
            If str1 <> vbNullString Then MsgBox str1 
        Loop
    End Sub
      

  3.   

    Function GetLeftWords(s As String, ByVal Ch As String) As String
        Dim i As Long
        i = InStr(s, Ch)
        If i > 0 Then
            GetLeftWords = Left(s, i - 1)
            s = Mid(s, i + Len(Ch))
        Else
            GetLeftWords = s
            s = vbNullString
        End If
    End Function
    Private Sub Command1_Click()
        Dim str,str1 as String
       
        str="100,300,宋体,15"
        Do While Len(str) > 0
            str1 = GetLeftWords(str, ",")
            If str1 <> vbNullString Then MsgBox str1 
        Loop
    End Sub
      

  4.   

    Split函数很好用啊,可以自定义分隔符,然后把分割结果存入数组,我以前用过。
      

  5.   

    to  weiweiplay(虚幻)
     你的代码我看过了,能否在函数中增加一个索引,可以选择第几个字符。另外,楼上说split函数是怎么用的呀?