此ASP InStr函数是否等效于js indexOf函数:
ASP:
<%
Function validation(ByVal var2)
If (InStr(var1, "," & var2 & ".") = 0) Then
validation = 1
Exit Function
End If
End Function
%>
JS:
validation = function(var2) {
if (var1.indexOf("," + var2 + ".") == -1) {
ValidateAction = 1;
};
return ValidateAction;
};
答案 0 :(得分:4)
不,它们仅在一种情况下相同。如果找不到值,则InStr返回0,indexOf返回-1,但是Instr还有很多其他内容,请看
差异
InStr函数返回一个字符串在另一个字符串中首次出现的位置。
InStr函数可以返回以下值:
If string1 is "" - InStr returns 0
If string1 is Null - InStr returns Null
If string2 is "" - InStr returns start
If string2 is Null - InStr returns Null
If string2 is not found - InStr returns 0
If string2 is found within string1 - InStr returns the position at which match is found
If start > Len(string1) - InStr returns 0
而javascript indexOf()
方法返回字符串中第一次出现指定值的位置。
This method returns -1 if the value to search for never occurs.