{"id":4871,"date":"2021-04-13T13:24:55","date_gmt":"2021-04-13T12:24:55","guid":{"rendered":"https:\/\/solidt.eu\/site\/?p=4871"},"modified":"2021-04-13T13:24:56","modified_gmt":"2021-04-13T12:24:56","slug":"limit-string-by-byte-count","status":"publish","type":"post","link":"https:\/\/solidt.eu\/site\/limit-string-by-byte-count\/","title":{"rendered":"Limit String By Byte Count"},"content":{"rendered":"\n<p>Source: <a href=\"https:\/\/codereview.stackexchange.com\/a\/55114\">https:\/\/codereview.stackexchange.com\/a\/55114<\/a><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">void Main()\n{\n\tconst string Input = \"a\\u0304\\u0308bc\\u0327\";\n\tvar bytes = Encoding.UTF8.GetByteCount(Input);\n\tConsole.WriteLine(\"{0} ({1} bytes in UTF-8)\", Input, bytes);\n\tfor (var i = 0; i &lt;= bytes; i++)\n\t{\n\t    var result = LimitBytes(Input, Encoding.UTF8, i);\n\t    Console.WriteLine(\"{0} \\\"{1}\\\" {2}\", i, result, Input.StartsWith(result, StringComparison.Ordinal));\n\t}\n}\n\npublic static string LimitBytes(string input, Encoding encoding, int maxBytes)\n{\n    if (maxBytes == 0 || string.IsNullOrEmpty(input)) return string.Empty;\n    if (encoding.GetByteCount(input) &lt;= maxBytes) return input;\n    var sb = new StringBuilder();\n    var bytes = 0;\n    foreach (char c in input)\n    {\n        bytes += encoding.GetByteCount(c.ToString());\n        if (bytes > maxBytes) break;\n        sb.Append(c);\n    }\n    return sb.ToString();\n}\n\n\/* OUTPUT:\na\u0304\u0308bc\u0327 (9 bytes in UTF-8)\n0 \"\" True\n1 \"a\" True\n2 \"a\" True\n3 \"a\u0304\" True\n4 \"a\u0304\" True\n5 \"a\u0304\u0308\" True\n6 \"a\u0304\u0308b\" True\n7 \"a\u0304\u0308bc\" True\n8 \"a\u0304\u0308bc\" True\n9 \"a\u0304\u0308bc\u0327\" True\n*\/<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Source: https:\/\/codereview.stackexchange.com\/a\/55114<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-4871","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/4871","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/comments?post=4871"}],"version-history":[{"count":1,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/4871\/revisions"}],"predecessor-version":[{"id":4872,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/4871\/revisions\/4872"}],"wp:attachment":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/media?parent=4871"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/categories?post=4871"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/tags?post=4871"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}