I would like a function that detects invalid characters in a URL and replaces them with their encoded equivalent. E.G.:
ensureValidUrl("http://example.com/invalid url/") // "http://example.com/invalid%20url/"
I had tried URLEncoder.encode, but this also encodes the protocol, which I don't want.