admin管理员组

文章数量:1389768

Is this ASP InStr function equivalent to the js indexOf function :

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;
};

Is this ASP InStr function equivalent to the js indexOf function :

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;
};
Share Improve this question asked Nov 1, 2019 at 12:35 user3470686user3470686 311 silver badge3 bronze badges 2
  • If you are only interested in checking if var2 (with , before and . after) exists within var1, then they are equivalent, yes. – Étienne Laneville Commented Nov 1, 2019 at 16:54
  • OP problem solved ! – Bilal Siddiqui Commented Jul 25, 2020 at 6:13
Add a ment  | 

1 Answer 1

Reset to default 4

No, they are same only with one case. If value not found InStr return 0 and indexOf return -1, but Instr has many other, have a look on

Differences

The InStr function returns the position of the first occurrence of one string within another.

The InStr function can return the following values:

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

Whereas javascript indexOf() method returns the position of the first occurrence of a specified value in a string.

This method returns -1 if the value to search for never occurs.

本文标签: javascriptInStr vs indexOfStack Overflow