CS-7621: Suggested optimization in escape function

Optimize the escape string function by using string rather than string builder and copying substring rather than appending each character.