Skip to main content
nbautista
Known Participant
July 3, 2009
Question

Is it possible to check if a complete string or part of it is in an array?

  • July 3, 2009
  • 2 replies
  • 1102 views

I had to check a text input from a user to se if is inside an array, the array contains single words with possible answers. What I need to know is; if somebody for example type the "sky is blue" and the array contains "blue", I want it to recognize it even is there's more words in the given string that are not in the array.

If is possible what is the sintax?

Other question I have is about case-sensitive, from my understanding arrays are not case-sensitive until we say so.

Thanks!

This topic has been closed for replies.

2 replies

Ned Murphy
Legend
July 3, 2009

What you would probably want to do is break the user's input string into individual words using the split method, and then take the resulting array and for each word of that string use the indexOf method to see if it is in the other array you want to check against.

As far as I know, arrays have nothing to do with case.  Strings do though.

nbautista
nbautistaAuthor
Known Participant
July 3, 2009

Hi Ned,

How do you break the user's input into individual words?

//this is the array

var answerOne:Array = new Array("", "expand", "grow", "grew", "renovate", "renovated", "upgrade", "upgraded", "remodel", "remodeled", "invest", "invested", "investments", "invested");

//this checks in the array for the input
if (answerOne.indexOf(answ_input.text)>0){

     }

if the user inputs "I upgraded it", it won't return true it will if user inputs "upgraded"

kglad
Community Expert
Community Expert
July 3, 2009

there's no need to split into an array:

//this is the array

var answerOne:Array = new Array("", "expand", "grow", "grew", "renovate", "renovated", "upgrade", "upgraded", "remodel", "remodeled", "invest", "invested", "investments", "invested");

answercorrectF(answerOne,answ_input.text);  // this is how to call answercorrectF()

// nothing below needs to be changed

function answercorrectF(a:Array,s:String):Boolean{

for(var i:uint=0;i<a.length;i++){
if (s.indexOf(a)>=0){  // use  || a.indexOf(answ_input.text)>0 if you want to check for either

return true;

     }

}

return false;

}

kglad
Community Expert
Community Expert
July 3, 2009

you can use indexOf().