Expand A Numerical Input Range to All Values

Average rating
(0 votes)

This string function will take an input of numbers in the form S-F and expand it to all values. The separator is by default a comma, however the optional sep=STRING will make the separator be STRING.

The function illustrates both the use of an optional input and grep pattern matching in SplitString.

Example

ExpandString("9-12",sep="..") will return 9..10..11..12..

Function/S ExpandString(theStr, [sep])
	string theStr, sep
 
	string xs, xe
	variable ns, ne, ic
 
	if(ParamIsDefault(sep))
		sep = ","
	endif
 
	SplitString/E="(.+)-(.+)" theStr, xs,xe
 
	if (strlen(xs)==0)
		return (theStr + sep)
	endif
	ns = str2num(xs)
	ne = str2num(xe)
 
	theStr = ""
	for(ic=ns;ic<(ne+1);ic+=1)
		theStr += num2str(ic) + sep
	endfor
	return theStr
end

Back to top