Refactor getKeyString functions to extract common code.