Thank you Daniel for your test. I am not sure whether there are still bugs in my codes.
And thank you alanjt, my codes are mainly based on yours and CAB's codes. I changed the "2" in your code to "(strlen delim)" to be applicable for multi-char delim