Created
June 18, 2018 16:01
-
-
Save karoltheguy/08d639aa8d5d3228a7352a0020962f8e to your computer and use it in GitHub Desktop.
StringExtension URLEncode
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static string UrlEncode(this string input) | |
{ | |
const int maxLength = 32766; | |
if (input == null) | |
throw new ArgumentNullException("input"); | |
if (input.Length <= maxLength) | |
return Uri.EscapeDataString(input); | |
var sb = new StringBuilder(input.Length * 2); | |
var index = 0; | |
while (index < input.Length) | |
{ | |
var length = Math.Min(input.Length - index, maxLength); | |
var subString = input.Substring(index, length); | |
sb.Append(Uri.EscapeDataString(subString)); | |
index += subString.Length; | |
} | |
return sb.ToString(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment