Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Aug 2010
    Location
    Now Southern Oregon. I was born and had lived my life in Los Angeles until relocating last year (2010)
    Posts
    213
    Thanks
    52
    Thanked 1 Time in 1 Post

    index values of strings as string or int?

    I am now working on a utility function that will gather the indexes for substring matches found in a string. There is an academic mystery I am
    puzzling over
    Code:
    function getMatchIndex(a, b) // a: sting to search b: what to search for
             {
              var out = new Array();
              if(a.lastIndexOf(b) > -1) // at least two matches, ****! there could be more
                {
                 if( (a.substr(a.indexOf(b) + b.length, a.lastIndexOf(b))).length > b.length )
                   {
                     var stp = parseInt(a.lastIndexOf(b)) - (parseInt(a.indexOf(b)) + parseInt(b.length));
                     alert(stp)
                     alert( (a.substr(a.indexOf(b) + b.length, stp)).length+' : '+a.substr(a.indexOf(b) + b.length, stp));
                   }
                 else
                   {
                    out[out.length] = a.indexOf(b);
                    out[out.length] = a.lastIndexOf(b);
                    return out;
                   }
                }
              else if(a.indexOf(b) > - 1 && a.lastIndexOf(b) == -1)
                {
                 out[out.length] = a.indexOf(b);
                 return out;
                }
              else if(a.indexOf(b) == -1)
                {
                 out = 'no matches';
                 return out;
                }
              else
                {
                 var splitStr = new Array();
                     splitStr = a.split(b);
                 /*
                  possibilities:
                  1: one match at the begining or end
                     -- leaves one significant item
                  2: one match somewhere after the beginning and before the end.
                     -- gives two matches.
                 */
                 for(var i = 0; i < splitStr.length - 1; i++) // don't want the last segment length
                    {
                     out[out.length] = splitStr[i].length
                    }
                }
             }
    getMatchIndex('xxxi5ixxxxxxxi5ixx', 'i5i');
    in the following code snippet:
    Code:
     if( (a.substr(a.indexOf(b) + b.length, a.lastIndexOf(b))).length > b.length )
                   {
     var stp = parseInt(a.lastIndexOf(b)) - (parseInt(a.indexOf(b)) + parseInt(b.length));
                     alert(stp)
                     alert( (a.substr(a.indexOf(b) + b.length, stp)).length+' : '+a.substr(a.indexOf(b) + b.length, stp));
    I am trying to get the sub string between the end of the first match to the beginning of the last match.
    The 'if' test passes, but I have to parseInt the values to get the proper
    value for stp.

    SO, the question is, what is the actual data type of an index value obtained from a string. (It would appear to be a string; having to use it an a math expression doesn't work without parseInt).
    (I hope I haven't posted too much code)
    Thank you in advance.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,919
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by anotherJEK View Post
    SO, the question is, what is the actual data type of an index value obtained from a string. (It would appear to be a string; having to use it an a math expression doesn't work without parseInt).
    The index value is a number:-


    Code:
    <script type = "text/javascript">
    
    var str = "Philip";
    var x = str.lastIndexOf("i");
    alert (typeof x);  // number
    var y = x + 25;
    alert (y);  // 29
    
    var a = "Philip";
    var b = "i";
    var stp = a.lastIndexOf(b) - a.indexOf(b) + b.length;  // 4-2+1 = 3
    alert(stp);  // ili
    alert( (a.substr(a.indexOf(b) + b.length, stp)).length+' : '+a.substr(a.indexOf(b) + b.length, stp)); //3:lip
    
    </script>

    Quizmaster: What is the last letter of the Greek alphabet?
    Contestant: Zulu
    Last edited by Philip M; 01-01-2012 at 08:02 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    anotherJEK (01-01-2012)

  • #3
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    indexOf / lastIndexOf return an integer.
    Quote Originally Posted by anotherJEK View Post
    Code:
              if(a.lastIndexOf(b) > -1) // at least two matches, ****! there could be more
    lastIndexOf does not require two matches to be present to find one.
    (I hope I haven't posted too much code)
    No - you just wrote too much.

    Code:
    <script type="text/javascript">
    
    function getMatchIndices(a, b) // a: string to search b: what to search for
    {
       var result = [],
           idx = 0,
           p;
    
       while( ( p = a.substring( idx ).indexOf( b ) ) > -1 )
       {
         result.push( p + idx );
         idx += p + b.length;     
       }
    
       return result;
    }
    
    alert( getMatchIndices( 'xxxi5ixxxxxxxi5ixx', 'i5i' ) );
     
    </script>

  • Users who have thanked Logic Ali for this post:

    anotherJEK (01-01-2012)

  • #4
    Regular Coder
    Join Date
    Aug 2010
    Location
    Now Southern Oregon. I was born and had lived my life in Los Angeles until relocating last year (2010)
    Posts
    213
    Thanks
    52
    Thanked 1 Time in 1 Post

    Well..

    To Philip M
    I get what you did. I am wondering what it is that I was doing wrong.
    I will go back over it.

    To Logic Ali

    I finalized my code, but it is still fairly involved. You can see my level
    of expertise. I will have to study your example to understand it.

    Thank you for info and suggestions.
    JK


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •