Careful, if you don't use a CLI you have to come up with your own new, custom solution to making your code hard to use and your features undiscoverable.
Stop reinventing the wheel people!
You’re running out of memory because you are copying the giant string somewhere. If you never alter the string, but instead work along it using the “offset” inputs on the string functions, then you can use all the loops you like.