View Full Version : String.Find
Jason Pate
03-27-2007, 11:07 AM
Any ideas for how to get a count of the number of substrings in a string.
Maybe a String.FindCount function?
I have a files with "_" as a field seporator (Example: "BAJANGO_ARONNAX_4654474_PTRE_20070117.pdf") but clients can add additional fields so the given number of "_" can change don’t want to recode this part every time, a String.FindCount that returns a count of a repeated substrings. So one can create a loop.
Use the DelimitedStringToTable with "_" as the delimiter, then get the table count.
Arguments: DelimitedString - The string to parse
Delimiter - The delimiter
Returns: A numerically indexed table with the string elements
Example:
string = "Brett,Mark,Darryl,Lorne,Adam";
tbResults = DelimitedStringToTable(string,",");
for i, name in tbResults do
Dialog.Message("Name",name);
end
]]--
function DelimitedStringToTable(DelimitedString, Delimiter)
tbReturn = {};
local strWorking;
local nPos = nil;
local strData;
local nTableIndex = 1;
local nDelimiterLength = String.Length(Delimiter);
if(nDelimiterLength < 1)then
tbReturn[nTableIndex] = DelimitedString;
return tbReturn;
end
strWorking = DelimitedString;
nPos = String.Find(strWorking,Delimiter);
while(nPos ~= -1)do
strData = String.Left(strWorking,nPos-1);
tbReturn[nTableIndex] = strData;
nTableIndex = nTableIndex + 1;
local nLength = String.Length(strWorking);
strWorking = String.Right(strWorking,nLength - (nPos + (nDelimiterLength-1)));
nPos = String.Find(strWorking,Delimiter);
end
if(strWorking ~= "")then
tbReturn[nTableIndex] = strWorking;
end
return tbReturn;
end
Jason Pate
03-27-2007, 11:37 AM
Nice work man, I am going to give this a try but looking at the code I think you have it nailed.
I can't take credit for the function, that's an IR goodie that comes in the Scriplets of AMS. Not sure if its in SF7 or not. Anyhow, hope you're good to go.
Jason Pate
03-27-2007, 04:22 PM
Yes I already have it working (just a few customization and one minor modification), You saved me from having to recreate it myself. Scriplets you say I will have to look see, I am AMS5/6 as well as SF7.
another one, probably faster, if not - at least shorter :)
function subStrCount(strInp, strPattern)
return (String.Length(strInp)- String.Length(String.Replace(strInp,strPattern,"",true))) / String.Length(strPattern);
end
replace true with false for case insensitive search, or make it a parameter
vBulletin® v3.8.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.