It appears that the result is the position of the first occurrence, plus (i-1). My understanding of start position (and the way it works in ‘that other program’) is that we’re looking for the search string, starting at that start position.
Is this a bug, or am I just misunderstanding the parameter(s)?
i agree that the results are not consistent with how the parameters should return a value.
The occurrence parameter seems to return correct positions as long as the start position is 1 but it all goes awry as soon as you change the start position !
You posted an incomplete code sample. It all depends on what you set your initial value to (default is 0). If you start at 1, then it will give you the results you expect.
Regardless of whether you loop or declare a variable, how are these “results you expect”?
F’rinstance, how can
var pos = utils.stringPosition('This is a test','s',5,1)//
bring back a result of 8?
Or how can
var pos = utils.stringPosition('This is a test','s',7,1)//
bring back 10?
Zero doesn’t make sense as a parameter, of course, but the only time I get the result I expect is when i = 1. Are you getting different results, or do you have an explanation for the results we’re getting for values other than 1?
It may still be that the parameters are just different than what I’m used to in FM, but if it’s supposed to be the same, then we have a bug. (A small one, though; a gnat, say… or a mite. )
I just hate to go straight to the Bugs and Issues folder. That’s where I often find that the issue has to do with the fool at the keyboard.
Sure looks like a bug to me. (will inform dev team)
For the time being, here’s a workaround
(I believe it resembles the functionality as found in FMP’s String Position function, but please test it yourself before using in production)
create a global script called “stringPosition”
var String = arguments[0];
var searchString = arguments[1];
var startPos = arguments[2];
var occurence = arguments[3];
var occCounter = 0;
var result= 0;
for( var i=0 ; i<String.length ; i++)
{
if(i<startPos-1)
{continue}
else if(String.substr(i,searchString.length)== searchString)
{
occCounter ++;
if(occCounter==occurence)
{
result = i+1;
break;
}
}
}
return result;
Call this global script from other scripts like:
var x = globals.stringPosition("This is a test","s",3,2)//result is 7
Actually, I’m realizing just now that this has wider implications than I thought.
Can you verify for me whether this will be left or changed back, before I go through the rest of my methods and calcs?
It really would make more sense to have it count from position 1. This would be consistent not only with other programs, but with the other string functions like stringMiddle and so forth.