Txt-Extract char separated

From Vectorlab
Jump to: navigation, search


Function to extract a string containing values separated by a char "ch". The routine extracts the first found value and overwrite the variable "str" deleting it. To work well the string passed should end with the search string. By Orso B. Schmid


{ Orso *********************************************** }
{ extracts values from a strings, separated by a char. Result is string }
{ every time a value is extracted, the value is deleted from the input string }
{ if the value is not found, the string doesn't get overwritten }
{ Example: ExtractCharSep('aaa;bbb;ccc;', ';'} 
{ first run --> returns 'aaa' and sets the variable "str" to 'bbb;ccc;' }
{ second run --> returns 'bbb' and sets the variable "str" to 'ccc;' }
{ third run --> returns 'ccc' and sets the variable "str" to '' }
FUNCTION T_ExtractCharSep(VAR str: STRING; ch: CHAR): STRING;
	VAR
		posStr : INTEGER;
		
	BEGIN
		T_ExtractCharSep := '';
		
		{ char sep at start deleted }
		WHILE (Len(str) > 1) & (Pos(ch, str) = 1) DO
			str := Copy(str, 2, Len(str));
			
		IF Len(str) > 0 THEN BEGIN
			posStr := Pos(ch, str);
			
			IF posStr > 0 THEN BEGIN
				T_ExtractCharSep := Copy(str, 1, posStr-1);
				Delete(str, 1, posStr);
			END ELSE
				T_ExtractCharSep := str;
		END;
	END;