Updates #trimAndCollapseWhitespaces to trim and replace after space replacement