Txt-Search replace suffix

From Vectorlab
Jump to: navigation, search


Function to search replace a string at the end of another string. Returns the resulting string. By Orso B. Schmid


{ Orso *********************************************** }
{ search replace string only found at end of the string }
{ warning! this can output an empty string! }
FUNCTION T_SearchReplaceSuffix(sourceStr, searchSuff, replSuff: STRING): STRING;
	VAR
		posInStr : INTEGER;
		temp_s : STRING;
		
	BEGIN
		T_SearchReplaceSuffix := sourceStr;
		posInStr := Len(sourceStr) - Len(searchSuff) + 1;
		
		IF posInStr > 0 THEN BEGIN
			temp_s := Copy(sourceStr, posInStr, Len(sourceStr));
			
			IF temp_s = searchSuff THEN
				T_SearchReplaceSuffix := Concat(Copy(sourceStr, 1, posInStr-1),
				replSuff);
		END;
	END;