PDA

View Full Version : String.CountDelimitedStrings


dtb
02-25-2005, 05:05 PM
Hi,

What would be the equivalent in AMS5 of the String.CountDelimitedStrings in AMS4?

thanks

TJ_Tigger
02-25-2005, 05:53 PM
There isn't one per se. You can use the function that were included with AMS5 to do change a delimited string to a table and then use Table.Count


--[[
Function: DelimitedStringToTable
Purpose: Breaks up a string given a delimiter into a table
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
Dislog.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, 1, false);
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

--[[
Function: TableToDelimitedString
Purpose: Makes a table into a delimited string
Arguments: Table - The table to make into a string
Delimiter - The delimiter
Returns: A delimited string of the table elements
Example:

tbNames = {};
tbNames[1] = "Brett";
tbNames[2] = "Mark";
tbNames[3] = "Darryl";
tbNames[4] = "Lorne";
tbNames[5] = "Adam";

strOutput = TableToDelimitedString(tbNames,",");
Dialog.Message("Result",strOutput);

]]--
function TableToDelimitedString(Table, Delimiter)
strReturn = "";
for i, item in Table do
strReturn = strReturn..item..Delimiter;
end
strReturn = String.TrimRight(strReturn,Delimiter);
return strReturn;
end



or you could make your own function that would count the number of delimited sections in a string.

function String.CountDelimitedStrings(sDelimitedString, sDelimiter)
local nReturn = 0
local nPos = nil
local nDelimiterLength = String.Length(Delimiter);

if sDelimitedString == "" then
return 0;
end

if sDelimiter == "" then
return 0;
end

nPos = String.Find(sDelimitedString, sDelimiter, 1, false);
while (nPos ~= -1) do
nReturn = nReturn +1;
nPos = String.Find(sDelimitedString, sDelimiter, nPos + nDelimiterLength, false);
end
return nReturn
end


or something along those lines anyway. I have not tested the above code but give it a shot and see if it works for you.

Tigg

dtb
02-25-2005, 06:55 PM
THANKS!

wow it look easy now :)