<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<oembed>
  <author_name>tmtms</author_name>
  <author_url>https://blog.hatena.ne.jp/tmtms/</author_url>
  <blog_title>tmtms のメモ</blog_title>
  <blog_url>https://blog.tmtms.net/</blog_url>
  <categories>
    <anon>Ruby</anon>
  </categories>
  <description>文字エンコーディングにUTF-8を使用した場合、1文字は1バイト〜4バイトです。 ある文字列の先頭からn文字の文字列を取り出すには次のようにできます。 str = &quot;本日は晴天なり&quot; n = 3 str[0, n] #=&gt; &quot;本日は&quot; 先頭からnバイトを超えない最大の文字列を取り出す方法を考えてみました。 愚直に数える str = &quot;本日は晴天なり&quot; n = 10 out = &quot;&quot; str.each_char do |c| break if out.bytesize + c.bytesize &gt; n out.concat c end out #=&gt; &quot;本日は&quot; inject を使うと少しかっこ…</description>
  <height>190</height>
  <html>&lt;iframe src=&quot;https://hatenablog-parts.com/embed?url=https%3A%2F%2Fblog.tmtms.net%2Fentry%2F2016%2F07%2F27%2Fruby-string-byte-length&quot; title=&quot;Rubyで指定バイト数を超えない文字列の取得 - tmtms のメモ&quot; class=&quot;embed-card embed-blogcard&quot; scrolling=&quot;no&quot; frameborder=&quot;0&quot; style=&quot;display: block; width: 100%; height: 190px; max-width: 500px; margin: 10px 0px;&quot;&gt;&lt;/iframe&gt;</html>
  <image_url></image_url>
  <provider_name>Hatena Blog</provider_name>
  <provider_url>https://hatena.blog</provider_url>
  <published>2016-07-27 13:22:40</published>
  <title>Rubyで指定バイト数を超えない文字列の取得</title>
  <type>rich</type>
  <url>https://blog.tmtms.net/entry/2016/07/27/ruby-string-byte-length</url>
  <version>1.0</version>
  <width>100%</width>
</oembed>
