Forum Discussion

soda's avatar
soda
Contributor
13 years ago

Is there a way to search for an exact match in a string?

Hello,

I've got an array that has some items from a listview stored in it.  The entries look similar to this:



Smart Collection  2

Ratings    5

Colors    0

Collection  4

Publish Collection  5



I need to check each entry to verify that the correct number follows the entry.  In a data file I've got expected results.  Currently I am using indexOf to see if the expected item is in the array.  When I am searching for "Collection" it is finding the "Smart Collection" item.  Is there a way to perform an exact match when using indexOf?  Or, I have seen people suggest using the search method, however, I am not familiar with regular expressions which is what  the search method apparently needs?  I'll go look through some more examples of regex but have so far not found examples of what I am trying to do.  Does TC have anything like this builtin?  Thanks for any suggestions.

Mark

5 Replies

  • tinauser's avatar
    tinauser
    Frequent Contributor
    I´m also not a great expert of regexp, but this should do what you need

    ^Collection$
  • AlexeyK's avatar
    AlexeyK
    SmartBear Alumni (Retired)

    Mark,


    What script code do you use for the search? Could you post it here?

    The solution depends on it. For instance, if the indexOf method is added to the JScript Array object via prototype, you may need to look at the prototype code. Perhaps, it does not use the === operator for comparison.

  • soda's avatar
    soda
    Contributor
    Thanks for the replies. I am going to try the ^Collection$ expression as well.



    What I have had for code so far is this:

    if((str.search(collectionSet) > -1) && str.search(collectionSet) < 1){

       - statements

    }



    and then decided I could just use:

    if((str.search(collectionSet) == 0){

      -statements

    }



    But I am concerned about a case where for whatever reason there might be a space before the actual listview item name sotred in my array.  In which case, I guess I could strip leading spaces.



    Any thoughts on that?
  • AlexeyK's avatar
    AlexeyK
    SmartBear Alumni (Retired)

    Mark,


    As far as I know, the search method finds a substring in a string using regular expressions.

    It's possible to use regular expressions here, but I think that they work longer and require a bit more resources.

    Why don't you use indexOf or ordinary comparison? E.g. --


        if(str.indexOf(collectionSet) == 0){

          ...


    -- or --


        if(str == collectionSet){

          ...


    The latter case implies that you have "full strings" specified in your baseline Excel sheet (that is, you have both "Smart Collection" and "Collection", not just "Collection").

  • soda's avatar
    soda
    Contributor
    Thanks Alex,

    I started out using indexOf, and then thinking I was going to need to use regular expressions, I switched to .search.  I think you helped confirm that I can simply just go back to indexOf.  I thought I had a scenario that would require regex, but, this solution should work.  Thanks.