我想檢查一個地址在asp中是否以http://www.youtube.com開頭。檢查經典ASP中的字符串「開頭」是否是另一個字符串?
如果我有這樣的事情
if rs("mainVideoName")="http://www.youtube.com*" then
這不工作,所以我該怎麼辦呢?
謝謝!
我想檢查一個地址在asp中是否以http://www.youtube.com開頭。檢查經典ASP中的字符串「開頭」是否是另一個字符串?
如果我有這樣的事情
if rs("mainVideoName")="http://www.youtube.com*" then
這不工作,所以我該怎麼辦呢?
謝謝!
可以使用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
,以檢查它與開始。
嘗試這種情況:
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來對大小寫敏感。
......怎麼
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
我認爲小寫'L'是變量的糟糕名字,特別是當它用於代替數字時。在我意識到你不只是比較rs(「mainVideoName」)的第一個字母之前,我必須看兩遍。 –
字符串從1 VB索引? :( – 2012-05-03 08:21:37
如果找不到string2,InStr返回0. http://msdn.microsoft.com/en-us/library/wybb344c%28v=vs.85%29.aspx –
@JamieDixon:a)> 0測試string2在string1中是_contained_,但不是string1 _startswith_ string2(儘管考慮到URL上下文_contains_可能就足夠了)b)在VBScript中,你不能在一個語句中定義和分配一個變量。 – AnthonyWJones