Txt-Extract char separated number

From Vectorlab
Jump to: navigation, search


Function to extract numbers separated by a char. Frequently used for dialog sizes stored in strings in the preference files. This is similar to T_ExtractCharSep but returns a valid real, if any. By Orso B. Schmid


{ Orso *********************************************** }
{ extracts values from a strings, separated by a char. Result is number or 0 }
{ every time a value is extracted, the value is deleted from input string }
{ Example: ExtractCharSepNum('125;234;456;', ';'} 
{ first run --> returns '125' and sets the variable "str" to '234;456;' }
{ second run --> returns '234' and sets the variable "str" to '456;' }
{ third run --> returns '456' and sets the variable "str" to '' }
FUNCTION T_ExtractCharSepNum(VAR str: STRING; ch: CHAR): REAL;
	VAR
		posStr : INTEGER;
		val : REAL;
		
	BEGIN
		T_ExtractCharSepNum := 0;
		IF Len(str) > 0 THEN BEGIN
		
			{ the case where you have only a number, without separator }
			{ for example: '125' }
			IF ValidNumStr(str, val) THEN BEGIN
				T_ExtractCharSepNum := val;
				str := '';
			
			{ otherwise extract the numbers }
			END ELSE BEGIN
				posStr := Pos(ch, str);
				
				IF (posStr > 0) & ValidNumStr(Copy(str, 1, posStr-1), val) THEN BEGIN
					T_ExtractCharSepNum := val;
					Delete(str, 1, posStr);
				END;
			END;
		END;
	END;