Txt-Search replace char

From Vectorlab
Jump to: navigation, search


Function to search replace a char in a string. Returns the modified string. By Orso B. Schmid


{ Orso *****************************************}
{ search replace chars }
{ warning! this can output an empty string! }
FUNCTION T_SearchReplaceChar(sourceStr: STRING; searchChar, replChar: CHAR): STRING;
		VAR
			str : STRING;
			from : INTEGER;
			
		BEGIN
			T_SearchReplaceChar := sourceStr;
			
			IF searchChar <> replChar THEN BEGIN
				str := sourceStr;
				from := Pos(searchChar, str);
				
				WHILE from > 0 DO BEGIN
					Delete(str, from, 1);
					
					IF Ord(replChar) > 0 THEN
						Insert(replChar, str, from);
					from := Pos(searchChar, str);
				END;
			
				T_SearchReplaceChar := str;
			END;
		END;