...

View Full Version : index values of strings as string or int?



anotherJEK
01-01-2012, 06:05 AM
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


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:


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.

Philip M
01-01-2012, 08:59 AM
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:-



<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

Logic Ali
01-01-2012, 09:50 AM
indexOf / lastIndexOf return an integer.




if(a.lastIndexOf(b) > -1) // at least two matches, ****! there could be morelastIndexOf 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.


<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>

anotherJEK
01-01-2012, 08:46 PM
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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum