{"categories":["Ruby"],"type":"rich","provider_url":"https://hatena.blog","author_url":"https://blog.hatena.ne.jp/tmtms/","width":"100%","html":"<iframe src=\"https://hatenablog-parts.com/embed?url=https%3A%2F%2Fblog.tmtms.net%2Fentry%2F2016%2F07%2F27%2Fruby-string-byte-length\" title=\"Ruby\u3067\u6307\u5b9a\u30d0\u30a4\u30c8\u6570\u3092\u8d85\u3048\u306a\u3044\u6587\u5b57\u5217\u306e\u53d6\u5f97 - tmtms \u306e\u30e1\u30e2\" class=\"embed-card embed-blogcard\" scrolling=\"no\" frameborder=\"0\" style=\"display: block; width: 100%; height: 190px; max-width: 500px; margin: 10px 0px;\"></iframe>","description":"\u6587\u5b57\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u306bUTF-8\u3092\u4f7f\u7528\u3057\u305f\u5834\u5408\u30011\u6587\u5b57\u306f1\u30d0\u30a4\u30c8\u301c4\u30d0\u30a4\u30c8\u3067\u3059\u3002 \u3042\u308b\u6587\u5b57\u5217\u306e\u5148\u982d\u304b\u3089n\u6587\u5b57\u306e\u6587\u5b57\u5217\u3092\u53d6\u308a\u51fa\u3059\u306b\u306f\u6b21\u306e\u3088\u3046\u306b\u3067\u304d\u307e\u3059\u3002 str = \"\u672c\u65e5\u306f\u6674\u5929\u306a\u308a\" n = 3 str[0, n] #=> \"\u672c\u65e5\u306f\" \u5148\u982d\u304b\u3089n\u30d0\u30a4\u30c8\u3092\u8d85\u3048\u306a\u3044\u6700\u5927\u306e\u6587\u5b57\u5217\u3092\u53d6\u308a\u51fa\u3059\u65b9\u6cd5\u3092\u8003\u3048\u3066\u307f\u307e\u3057\u305f\u3002 \u611a\u76f4\u306b\u6570\u3048\u308b str = \"\u672c\u65e5\u306f\u6674\u5929\u306a\u308a\" n = 10 out = \"\" str.each_char do |c| break if out.bytesize + c.bytesize > n out.concat c end out #=> \"\u672c\u65e5\u306f\" inject \u3092\u4f7f\u3046\u3068\u5c11\u3057\u304b\u3063\u3053\u2026","image_url":null,"version":"1.0","url":"https://blog.tmtms.net/entry/2016/07/27/ruby-string-byte-length","provider_name":"Hatena Blog","blog_title":"tmtms \u306e\u30e1\u30e2","blog_url":"https://blog.tmtms.net/","title":"Ruby\u3067\u6307\u5b9a\u30d0\u30a4\u30c8\u6570\u3092\u8d85\u3048\u306a\u3044\u6587\u5b57\u5217\u306e\u53d6\u5f97","height":"190","author_name":"tmtms","published":"2016-07-27 13:22:40"}