Search for information by content and not by array position

Asked

Viewed 319 times

0

I want to do a certain word search in another way, I just got from described below:

<%
mystring = "Como eu faço para separar uma string em várias strings?"
myarray = Split(mystring, " ")
For i = 0 to Ubound(myarray)
  Response.Write i & " - " & myarray(i) & "<br>"
  if myarray(0) = "Como" then
    existe = "Sim"
  Else
    existe = "Nao"
  end if
Next
Response.Write "Resposta Final >" & existe
%>

0 - Como
1 - eu
2 - faço
3 - para
4 - separar
5 - uma
6 - string
7 - em
8 - várias
9 - strings?

Final Answer >Yes

I want to search all over string which I have turned into an array, looking for no position myarray(0) zero, for example, but for myarray(i), but the result is "No", I need to use this structure in another project that uses Checkbox’s and I will never know which position will come. It is possible to change to the form myarray(i).

3 answers

2

Your result comes "No" because in the next step the answer is no. Once you find the result, you need to exit the loop:

<%
mystring = "Como eu faço para separar uma string em várias strings?"
myarray = Split(mystring, " ")
For i = 0 to Ubound(myarray)
  Response.Write i & " - " & myarray(i) & "<br>"
  if myarray(i) = "Como" then
    existe = "Sim"
    Exit For
  Else
    existe = "Nao"
  end if
Next
Response.Write "Resposta Final >" & existe
%>

Attention: untested code :)

0

you can use the split function. Follow the example:

<!DOCTYPE html>
<html>
<body>

<%

myString=Split("Como eu faço para separar uma string em várias strings?")
for each x in myString
   response.write(x & "<br>")
next

%>  

</body>
</html>

For more questions http://www.w3schools.com/vbscript/func_split.asp

0

Do you really need to turn into array? If you don’t need regular expression, it is much more effective. Below follows example in php, just convert to Asp.

$str = "Como eu faço para separar uma string em várias strings?";
$ret = preg_match("/eu/", $str);
//$ret retorna true no caso acima

Browser other questions tagged

You are not signed in. Login or sign up in order to post.