2012-05-03 77 views

回答

2

可以使用InStr()功能如下:

Dim positionOfMatchedString 
positionOfMatchedString= InStr(rs("mainVideoName"),"http://www.youtube.com") 

If positionOfMatchedString > 0 Then 
    // Do your stuff here 
End If 

安東尼指出,這告訴你,string2爲string1中包含。你可以寫爲:

If positionOfMatchedString = 1 Then 

,以檢查它開始。

+0

字符串從1 VB索引? :( – 2012-05-03 08:21:37

+0

如果找不到string2,InStr返回0. http://msdn.microsoft.com/en-us/library/wybb344c%28v=vs.85%29.aspx –

+0

@JamieDixon:a)> 0測試string2在string1中是_contained_,但不是string1 _startswith_ string2(儘管考慮到URL上下文_contains_可能就足夠了)b)在VBScript中,你不能在一個語句中定義和分配一個變量。 – AnthonyWJones

10

嘗試這種情況:

Function UrlStartsWith(string1, string2) 
    UrlStartsWith = InStr(1, string1, string2, 1) = 1 
End Function 

If UrlStartsWith(rs("mainVideoName"), "http://www.youtube.com") Then 


End If 

開始與通過使用IntStr並確保它返回1作爲搜索字符串中發現的起始位置進行測試。由於您正在測試URL,因此上面的代碼使用了一個TextCompare來對大小寫敏感。

1

......怎麼

Dim s: s = "http://www.youtube.com" 
Dim l: l = Len(s) 

If Left(rs("mainVideoName"), l) = s Then 
    ' String begins with URL part '   
End If 
+2

我認爲小寫'L'是變量的糟糕名字,特別是當它用於代替數字時。在我意識到你不只是比較rs(「mainVideoName」)的第一個字母之前,我必須看兩遍。 –

相關問題