0

Is it possible to use partial/substring match in the indexOf method of javascript ?

I have an String arraylist & i want to use partial text of the full string in the indexOf method to get the index number of the desired string in the arraylist.

var text[]=
0----This text over here $%*&*&*&(()(&$..: matches
1----%#@!$%*&*&*&(()(&$..: text to be matched
2----Where does this %#@!$%*&*&*&(()(&$..: belongs to any type of category
3----There are many %#@!$%*&*&*&(()(&$..: comparators

var index=text.indexOf("Where does this %#@!$%*&*&*&(()(&$..: belongs to any type of category"), instead of the above line i want to use something like:

var index=text.indexOf("belongs to any type of category") 

I need to get index number 2 based on the text match hit i.e "Where does this %#@!$%&&*&(()(&$..: belongs to any type of category" but bcz of the special characters inside the string it is making it tuff & since its an array having other strings which i am getting dynamically is adding to the complications.

So i am trying to use the .indexOf method in which we can pass a string & it returns the index number, so my question here is there a way i could pass in a part of String instead of the entire string & get the index number 2 successfully ?

Code Tried:

describe('angularjs homepage todo list', function() {
    var index = 'not found';
    var text1 = "";

    it('test', function() {
        var textToFind = "belongs to any type of category";
        for (var i=0;i<10;i++){
        var results_list=element.all(By.xpath("//*[@id='panel']/div/div[2]/span[1]")).get(i).getText().then(function(text) {  
            text1=text1+"\n"+text;
            console.log("promise returned text inside function is "+text1);
            return text1;   
        })
    }
        console.log('Text via global defined variable text1 is ' + text1);
        getIndex(0, text1.length, text1, textToFind);
        console.log('index is ' + index);
    });

    function getIndex(i, max, array, textToFind) {
        if (i < max) {
            console.log('text[' + i + '].indexOf = ' + array[i].indexOf(textToFind))
            if (array[i].indexOf(textToFind) > 0) {
                index = i;
                //The index number will be assigned to the variable index  
                //if indexOf is greater than 0, e.g. 38 was returned on index 2
           } else {
                getIndex(i + 1, max, array, textToFind);
           }
       }
    }
});


 Started
 Started execution of test


 Text via global defined variable text1 is 
 index is not found
 Test Case passed

 promise returned text inside function is ['This text over here $%*&*&*&(()(&$..: matches', 
    '%#@!$%*&*&*&(()(&$..: text to be matched',
    'Where does this %#@!$%*&*&*&(()(&$..: belongs to any type of category',
'There are many %#@!$%*&*&*&(()(&$..: comparators']

 1 spec, 0 failures
 Finished in 58.462 seconds


 [18:35:47] I/launcher - 0 instance(s) of WebDriver still running
 [18:35:47] I/launcher - internet explorerANY #01 passed
Automation Engr
  • 444
  • 2
  • 4
  • 26
  • This is not a normal one liner string ... its an array of strings and when the comparison is done using indexOf method it takes full value of the respective index. – Automation Engr May 03 '17 at 09:03
  • _Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the [How to Ask](http://stackoverflow.com/help/how-to-ask) page for help clarifying this question._ – hindmost May 03 '17 at 09:04
  • http://stackoverflow.com/questions/5424488/how-to-search-for-a-string-inside-an-array-of-strings check this this is the same as you asked – Nair Athul May 03 '17 at 09:05

1 Answers1

1

I need to totally update the answer:

describe('testing', function() {
    var index = 'not found';
    var text1 = [];

    it('should push element to array', function() {
        browser.ignoreSynchronization = true;
        browser.get('https://www.w3schools.com/angular/');
        var elm = element(by.id('leftmenuinner')).all(By.css('[target="_top"]'));
        elm.count().then(function(count) {
            pushToArray(0, count, elm);
        })

    });

    it('should identify index', function() {
        var textToFind = "Data Binding";
        getIndex(0, text1.length, text1, textToFind);
        console.log('Text via global defined variable text1 is ' + text1);
        console.log('index is ' + index);
    });

    function getIndex(i, max, array, textToFind) {
        if (i < max) {
                console.log('text[' + i + '].indexOf = ' + array[i].indexOf(textToFind))
                    if (array[i].indexOf(textToFind) > 0) {
                        index = i;
                        //The index number will be assigned to the variable index  
                        //if indexOf is greater than 0, e.g. 38 was returned on index 2
                } else {
                        getIndex(i + 1, max, array, textToFind);
                }
        }
    }

    function pushToArray(i, max, elm) {
        if (i < max) {
            elm.get(i).getText().then(function(tmpText) {
                console.log(tmpText);
                text1.push(tmpText);
            })
            pushToArray(i + 1, max, elm);
        }
    }
});

I hope the sample website I used fits well with what you are trying.
If this works, you can still update it to make the code shorter.

Paul Co
  • 447
  • 2
  • 9
  • the idea here is to get the index number based on the partial text match, your function line if (array[i].indexOf(textToFind) > 0) { index = i}; will return -1 as array[2] value is "Where does this %#@!$%*&*&*&(()(&$..: belongs to any type of category", i am looking for some approach were i could pass the substring "belongs to any type of category" & based upon partial text match get the index number as 2. Hope the problem is clear here. – Automation Engr May 05 '17 at 06:39
  • I don't understand you. the for loop on my example checks all the content of your `text` array. you mentioned from your question that there is 4 values in your array. on my code, text[0] will return -1, text[1] will return -1 and text[2] will return >1 and will assign the value of i to variable index which is 2. – Paul Co May 06 '17 at 04:39
  • text[2] from your code will return -1, bcz the value of text[2] is "Where does this %#@!$%*&*&*&(()(&$..: belongs to any type of category", what you have used in your code in the variable textToFind is just the partial text. The indexOf() method will fail to use this partial text for comparison & retrieval of the index number. – Automation Engr May 08 '17 at 06:52
  • if your still not able to understand the case problem here we could continue this conversation in a separate chat box or text me your mail id. Thanks – Automation Engr May 08 '17 at 06:53
  • I updated the answer and made it more detailed and executable on protractor for you to see the result in console. If it is still not what you are looking for, kindly update your question and provide more detailed information like the html, expected result etc. – Paul Co May 08 '17 at 11:07
  • More specifically i am looking to get the index number of the entire text line i.e index number-2 in this case .............. your code gets me the index number from where the line "belongs to any type of category" starts. so in short to be very precise after running the code it should return me the index number 2 and not 38. – Automation Engr May 08 '17 at 12:07
  • As you can see on my code.we have a global variable INDEX. It will contain the index number. If you checked the code, the last line is console.log(index), and in the cmd, the last value displayed is "2" – Paul Co May 08 '17 at 12:11
  • The array is creating problems here. Have a look at the code i have updated in my question body. – Automation Engr May 08 '17 at 13:16
  • Can you share the application? or if you know a site with similar format? – Paul Co May 08 '17 at 13:20
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/143739/discussion-between-vishal-and-paul-co). – Automation Engr May 09 '17 at 05:23
  • great :) happy to help – Paul Co May 09 '17 at 09:23