是一段搜索引擎的URL地址,
可能是这样:
http://www.soso.com/search?new=2&q=字符串在此&viewIndex=7&source=searchnew  (q=字符串在此 可能夹在几个参数中间)
也可能是这样:
http://www.soso.com/search?new=2&q=字符串在此    (q=字符串在此   后面可能是空的,没有参数)需要获取 "字符串在此"URL地址里的一些参数可能会变,但是有1个不变,就是肯定有: q=  而且仅有1个q=,不会有2个例如:q=xxx    ,就是提交搜索的字符串xxx一句话,就是需要截取   q=  后面字符如何处理,程序才高效?

解决方案 »

  1.   

    mid(str,instr(str,"q=") + 2)
      

  2.   

    URL地址中,各个参数的连接 是用 &&q=字符串在此&字符串在此  后面如果是 & 就不截取了或者是空的,也不截取了
      

  3.   


        Stri = "http://www.soso.com/search?new=2&q=字符串在此&viewIndex=7&source=searchnew"
        MsgBox Mid(Stri, InStr(Stri, "q=") + 2, InStr(InStr(Stri, "q="), Stri, "&") - InStr(Stri, "q=") - 2)- -||我写的什么东西。效率肯定很低
      

  4.   

    a = Split(Filter(Split(str, "&"), "q=")(0), "q=")(1)
    不知道这连个那个效率高
      

  5.   

    从效率来说,还是mid/replace 快。
      

  6.   

    讲效率就应该用搜索
    dim i1 as long
    dim i2 as long 
    i1 = instr(1, url, "q=")
    i2 = instr(i1+2, url, "&")
    if i2 = 0 then
        s = mid$(url, i1+2)
    else
        s = mid$(url, i1+2, i2-i1-2)
    end if 
      

  7.   

    执行前后加上时间,Debug.Print Time
      

  8.   

    instr是VB中最快的函数之一,而split正相反,相当慢....
      

  9.   

    使用一个for循环纪录下"q="然后纪录"q="后的"&"的位置,或者后面没有了到字符串尾然后再mid一下我觉得这样可以节省遍历字符串的次数应该效率高些,有点象c中的指针移动Option ExplicitPrivate Function GetString(ByVal strURL As String) As String
        Dim Length As Integer
        Dim i As Integer
        Dim Start As Integer
        Dim offset As Integer
        Dim offset1 As Integer
        
        Length = Len(strURL)
        
        If LCase(Mid(strURL, 1, 11)) = "http://www." Then
            Start = 12
        ElseIf LCase(Mid(strURL, 1, 6)) = "http://" Then
            Start = 7
        ElseIf LCase(Mid(strURL, 1, 4)) = "www." Then
            Start = 5
        Else
            Start = 1
        End If
        For i = Start To Length
            If Mid(strURL, i, 2) = "q=" Then
                offset = i + 2
            End If
            If offset Then
                If Mid(strURL, i, 1) = "&" Then
                    offset1 = i
                    Exit For
                End If
            End If
        Next
        
        If offset1 = 0 Then
            GetString = Mid(strURL, offset, Length - offset + 1)
        Else
            GetString = Mid(strURL, offset, offset1 - offset)
        End If
    End FunctionPrivate Sub Command1_Click()
        Dim strURL As String
        Dim strURL1 As String
        strURL = "http://www.soso.com/search?new=2&q=字符串在此&viewIndex=7&source=searchnew"
        strURL = GetString(strURL)
        strURL1 = "http://www.soso.com/search?new=2&q=字符串在此"
        strURL1 = GetString(strURL1)
    End Sub
      

  10.   

    vb效率真低,我上面的代码模仿c指针的效果和真正c写出的代码相差不知道多少倍去了VOID GetString(LPSTR lpURL,LPSTR lpOutString)
    {
    LPSTR Start,End;
    Start = lpURL;
    while (*Start){
    if (*Start=='q'){
    if (*(Start+1)=='='){
    Start+=2;
    break;
    }
    else{
    Start+=2;
    }
    }
    else{
    Start++;
    }
    }
    End = Start;
    while (*End){
    if (*End=='&'){
    break;
    }
    End++;
    }
    memcpy(lpOutString,Start,End-Start);
    } CHAR strURL[256]="http://www.soso.com/search?new=2&q=字符串在此&viewIndex=7&source=searchnew";
    CHAR strOutString[256]={0};
    DWORD dwTime=GetTickCount();
    for (int i=0;i<100000;i++){
    GetString(strURL,strOutString);
    }
    printf("花费时间:%d\n",GetTickCount()-dwTime);这段c代码需要时间仅仅是14-18之间,1000为一秒但是上面我写的vb代码时间,却要花费一秒多大概是1.2秒左右
      

  11.   

    #include "stdafx.h"
    #include "windows.h"
    #include <stdio.h>VOID GetString(LPSTR lpURL,LPSTR lpOutString)
    {
    LPSTR Start,End;
    Start = lpURL;
    while (*Start){
    if (*Start=='q'){
    if (*(Start+1)=='='){
    Start+=2;
    break;
    }
    else{
    Start+=2;
    }
    }
    else{
    Start++;
    }
    }
    End = Start;
    while (*End){
    if (*End=='&'){
    break;
    }
    End++;
    }
    memcpy(lpOutString,Start,End-Start);
    }VOID main()
    {
    CHAR strURL[256]="http://www.soso.com/search?new=2&q=字符串在此&viewIndex=7&source=searchnew";
    CHAR strOutString[256]={0};
    DWORD dwTime=GetTickCount();
    for (int i=0;i<100000;i++){
    GetString(strURL,strOutString);
    }
    }