Limit URL-encoded string lengths using multibyte characters in .NET.

I am using .NET and I need to truncate a string that may contain multibyte characters so that it does not exceed a given length after encoding the URL. This is similar to what will be built in, but I cannot find it.

I would just execute the substring after the URL is encoded, but this could take away part of the encoded character (the space becomes% 20, and if it were at the end, it could be truncated to% 2, which is invalid), or that part a multibyte character will be truncated (π gets the encoding as% CF% 80, and it can be truncated as%,% CF,% CF% 8, all of which are erroneous).

+5
source share
1 answer

My quick google search didn’t cause anything for this, which is somewhat surprising, since this seems like a pretty common problem (at least for those who don’t avoid monstrously long URLs).

You can do it iteratively where you encode the string, and if the encoded string is too long, you beat the character from the original and re-encode and continue to do so until the encoded string becomes short enough. This is obviously not very revealing.

+1
source

All Articles